9front - general discussion about 9front
 help / color / mirror / Atom feed
* [9front] str2json and jsonfmt
@ 2024-01-25  5:52 Xiao-Yong Jin
  2024-01-25  8:06 ` cinap_lenrek
  0 siblings, 1 reply; 13+ messages in thread
From: Xiao-Yong Jin @ 2024-01-25  5:52 UTC (permalink / raw)
  To: 9front

I wrote the following two json programs and found them helpful and
wanted to share with you.  Feel free to add it to the 9front
distribution if more people find them useful.

/* str2json.c */
#include <u.h>
#include <libc.h>
#include <json.h>

void
main(void)
{
	char *buf;
	long n;
	ulong m;
	struct JSON json;

	JSONfmtinstall();

	buf = nil;
	m = 0;
	for(;;){
		m += IOUNIT;
		buf = realloc(buf, m);
		if(!buf)
			sysfatal("realloc");
		n = readn(0, buf+m-IOUNIT, IOUNIT);
		if(n < IOUNIT){
			buf[m-IOUNIT+n] = '\0';
			break;
		}
	}

	json.t = JSONString;
	json.s = buf;

	print("%J", &json);

	free(buf);
	exits(nil);
}

/* jsonfmt.c */
#include <u.h>
#include <libc.h>
#include <json.h>

JSON*
jsonindex(JSON *j, vlong i)
{
	JSONEl *e;

	if(j->t != JSONArray){
		werrstr("not an array");
		return nil;
	}
	if(i < 0){
		werrstr("negative index");
		return nil;
	}
	for(e=j->first; e!=nil; e=e->next, --i)
		if(i==0)
			return e->val;
	werrstr("index out of bounds");
	return nil;
}

void
printjson(JSON *j, int raw)
{
	JSONEl *e;

	if(j->t == JSONString && raw)
		print("%s", j->s);
	else if(j->t == JSONArray && raw)
		for(e=j->first; e!=nil; e=e->next)
			print("%J\n", e->val);
	else
		print("%J\n", j);
}

void
usage(void)
{
	fprint(2, "usage: jsonfmt [-r] [names/indices...]\n");
	exits("usage");
}

void
main(int argc, char *argv[])
{
	char *buf;
	long n;
	ulong m;
	JSON *json, *obj;
	int i;
	int raw;

	JSONfmtinstall();

	buf = nil;
	m = 0;
	for(;;){
		m += IOUNIT;
		buf = realloc(buf, m);
		if(!buf)
			sysfatal("realloc");
		n = readn(0, buf+m-IOUNIT, IOUNIT);
		if(n < IOUNIT){
			buf[m-IOUNIT+n] = '\0';
			break;
		}
	}

	json = jsonparse(buf);
	if(!json)
		sysfatal("jsonfmt: %r");

	raw = 0;
	ARGBEGIN{
	case 'r':
		raw = 1;
		break;
	default:
		usage();
	}ARGEND

	if(argc == 0)
		printjson(json, raw);
	else for(i=0; i<argc; ++i){
		obj = nil;
		if(json->t==JSONObject)
			obj = jsonbyname(json, argv[i]);
		else if(json->t==JSONArray)
			obj = jsonindex(json, atoll(argv[i]));
		else
			werrstr("not a JSON array or object");
		if(obj)
			printjson(obj, raw);
		else
			sysfatal("jsonfmt: %r");
	}

	jsonfree(json);
	free(buf);
	exits(nil);
}


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] str2json and jsonfmt
  2024-01-25  5:52 [9front] str2json and jsonfmt Xiao-Yong Jin
@ 2024-01-25  8:06 ` cinap_lenrek
  2024-01-25  8:57   ` phil9
  0 siblings, 1 reply; 13+ messages in thread
From: cinap_lenrek @ 2024-01-25  8:06 UTC (permalink / raw)
  To: 9front

nice!

thanks for sharing.

this might indeed become usefull for a mangadex fetcher.

--
cinap

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] str2json and jsonfmt
  2024-01-25  8:06 ` cinap_lenrek
@ 2024-01-25  8:57   ` phil9
  2024-01-25  9:17     ` chris
                       ` (3 more replies)
  0 siblings, 4 replies; 13+ messages in thread
From: phil9 @ 2024-01-25  8:57 UTC (permalink / raw)
  To: 9front

I also wrote some json tool
(http://shithub.us/phil9/gj/HEAD/info.html) inspired by gron to make
json greppable.
umbraticus has a better version but I can never remember their site :)

Maybe we could have all these tools in a json subdir of /bin like
json/fmt, json/parse, json/grep

On Thu, Jan 25, 2024 at 9:08 AM <cinap_lenrek@felloff.net> wrote:
>
> nice!
>
> thanks for sharing.
>
> this might indeed become usefull for a mangadex fetcher.
>
> --
> cinap

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] str2json and jsonfmt
  2024-01-25  8:57   ` phil9
@ 2024-01-25  9:17     ` chris
  2024-01-25 14:03       ` Alex Musolino
  2024-01-25 13:42     ` Dave MacFarlane
                       ` (2 subsequent siblings)
  3 siblings, 1 reply; 13+ messages in thread
From: chris @ 2024-01-25  9:17 UTC (permalink / raw)
  To: 9front

> umbraticus has a better version but I can never remember their site :)

Perhaps this one http://runjimmyrunrunyoufuckerrun.com/src/jb.c

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] str2json and jsonfmt
  2024-01-25  8:57   ` phil9
  2024-01-25  9:17     ` chris
@ 2024-01-25 13:42     ` Dave MacFarlane
  2024-01-25 14:57       ` cinap_lenrek
  2024-01-25 14:19     ` hiro
  2024-01-26 14:53     ` ori
  3 siblings, 1 reply; 13+ messages in thread
From: Dave MacFarlane @ 2024-01-25 13:42 UTC (permalink / raw)
  To: 9front

I wrote something similar to OP's str2json which I call jsonfmt which
I use all the time, so I'm definitely in favour of including it.

If there's going to be a /bin/json/* hierarchy another candidate would
be a json/fs.  I have one that I wrote for myself here:
https://github.com/driusan/fedi9/blob/front/jsonfs.c to make it easier
to deal with json in shell scripts but I don't think it's very good or
up to 9front standards (it's not well documented and it can get
crash-y when you try and re-use same /mnt/json/ctl with different
json).  There may be other json/fs implementations out there in the
world.

Quoth phil9 <telephil9@gmail.com>:
> I also wrote some json tool
> (http://shithub.us/phil9/gj/HEAD/info.html) inspired by gron to make
> json greppable.
> umbraticus has a better version but I can never remember their site :)
> 
> Maybe we could have all these tools in a json subdir of /bin like
> json/fmt, json/parse, json/grep
> 
> On Thu, Jan 25, 2024 at 9:08 AM <cinap_lenrek@felloff.net> wrote:
> >
> > nice!
> >
> > thanks for sharing.
> >
> > this might indeed become usefull for a mangadex fetcher.
> >
> > --
> > cinap
> 


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] str2json and jsonfmt
  2024-01-25  9:17     ` chris
@ 2024-01-25 14:03       ` Alex Musolino
  0 siblings, 0 replies; 13+ messages in thread
From: Alex Musolino @ 2024-01-25 14:03 UTC (permalink / raw)
  To: 9front

Quoth chris@chrisfroeschl.de:
> > umbraticus has a better version but I can never remember their site :)
> 
> Perhaps this one http://runjimmyrunrunyoufuckerrun.com/src/jb.c

How can you not remember that?! :)


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] str2json and jsonfmt
  2024-01-25  8:57   ` phil9
  2024-01-25  9:17     ` chris
  2024-01-25 13:42     ` Dave MacFarlane
@ 2024-01-25 14:19     ` hiro
  2024-01-26 14:53     ` ori
  3 siblings, 0 replies; 13+ messages in thread
From: hiro @ 2024-01-25 14:19 UTC (permalink / raw)
  To: 9front

> Maybe we could have all these tools in a json subdir of /bin like
> json/fmt, json/parse, json/grep

it seems extremely trivial. but with the right convention this seems
surprisingly powerful.

super obvious in hindsight now that you thought it out loud. and
that's probably why i like it so much.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] str2json and jsonfmt
  2024-01-25 13:42     ` Dave MacFarlane
@ 2024-01-25 14:57       ` cinap_lenrek
  2024-01-25 15:39         ` Dave MacFarlane
  0 siblings, 1 reply; 13+ messages in thread
From: cinap_lenrek @ 2024-01-25 14:57 UTC (permalink / raw)
  To: 9front

very interesting.

i tried jg but the problem with mangadex json was that
they had newlines in the values, which where not escaped
so the grep approach doesnt work so well.

a file-system to parse it might do the trick.

--
cinap

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] str2json and jsonfmt
  2024-01-25 14:57       ` cinap_lenrek
@ 2024-01-25 15:39         ` Dave MacFarlane
  2024-01-25 16:16           ` Steve simon
  0 siblings, 1 reply; 13+ messages in thread
From: Dave MacFarlane @ 2024-01-25 15:39 UTC (permalink / raw)
  To: 9front


Quoth cinap_lenrek@felloff.net:
> very interesting.
> 
> i tried jg but the problem with mangadex json was that
> they had newlines in the values, which where not escaped
> so the grep approach doesnt work so well.
> 
> a file-system to parse it might do the trick.
> 
> --
> cinap
> 

*Not* escaped newlines? You mean their json looks something like:

"{
	"foo": "bar
baz"
}"?

That isn't valid JSON and even with a filesystem would (should) fail
when jsonparse() is called on it.  (The above example fails with
JSON.parse in both Firefox and Chrome, so if that is what you mean I'm
not sure how they do anything with it..)


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] str2json and jsonfmt
  2024-01-25 15:39         ` Dave MacFarlane
@ 2024-01-25 16:16           ` Steve simon
  2024-01-26  9:34             ` umbraticus
  0 siblings, 1 reply; 13+ messages in thread
From: Steve simon @ 2024-01-25 16:16 UTC (permalink / raw)
  To: 9front

Hi,

I wrote something similar a long time ago

https://9p.io/sources/contrib/steve/libjson.tbz       - json library for plan9
https://9p.io/sources/contrib/steve/json.tbz          - tools, only a json beautifier

there are similar ones for xml in a similar place.
The parsers are well tested (the XML one in particular) and pretty robust.

If they are of any use to anyone, feel free to take the code.

-Steve


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] str2json and jsonfmt
  2024-01-25 16:16           ` Steve simon
@ 2024-01-26  9:34             ` umbraticus
  0 siblings, 0 replies; 13+ messages in thread
From: umbraticus @ 2024-01-26  9:34 UTC (permalink / raw)
  To: 9front

> there are similar ones for xml in a similar place.

Yes, always meant to thank you for that & the docx/xlsx stuff.

Thanks!

Possibly distasteful to suggest adding these to 9front, and they
need a little love, but they are of more use than the doc2txt(1)
stuff that already is shipped.

umbraticus

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] str2json and jsonfmt
  2024-01-25  8:57   ` phil9
                       ` (2 preceding siblings ...)
  2024-01-25 14:19     ` hiro
@ 2024-01-26 14:53     ` ori
  2024-01-26 17:28       ` Steve Simon
  3 siblings, 1 reply; 13+ messages in thread
From: ori @ 2024-01-26 14:53 UTC (permalink / raw)
  To: 9front

Quoth phil9 <telephil9@gmail.com>:
> 
> Maybe we could have all these tools in a json subdir of /bin like
> json/fmt, json/parse, json/grep

Yes, I like this idea -- I think this would make sense.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] str2json and jsonfmt
  2024-01-26 14:53     ` ori
@ 2024-01-26 17:28       ` Steve Simon
  0 siblings, 0 replies; 13+ messages in thread
From: Steve Simon @ 2024-01-26 17:28 UTC (permalink / raw)
  To: 9front

makes sense to me too.

> On 26 Jan 2024, at 2:55 pm, ori@eigenstate.org wrote:
> 
> Quoth phil9 <telephil9@gmail.com>:
>> 
>> Maybe we could have all these tools in a json subdir of /bin like
>> json/fmt, json/parse, json/grep
> 
> Yes, I like this idea -- I think this would make sense.
> 

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2024-01-26 17:31 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-25  5:52 [9front] str2json and jsonfmt Xiao-Yong Jin
2024-01-25  8:06 ` cinap_lenrek
2024-01-25  8:57   ` phil9
2024-01-25  9:17     ` chris
2024-01-25 14:03       ` Alex Musolino
2024-01-25 13:42     ` Dave MacFarlane
2024-01-25 14:57       ` cinap_lenrek
2024-01-25 15:39         ` Dave MacFarlane
2024-01-25 16:16           ` Steve simon
2024-01-26  9:34             ` umbraticus
2024-01-25 14:19     ` hiro
2024-01-26 14:53     ` ori
2024-01-26 17:28       ` Steve Simon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).