Live data from Hacker News

FX: An interactive alternative to jq to process JSON

github.com

21–30 of 65 posts

Re: FX: An interactive alternative to jq to process JSON

#21
post #19

Quoted post unavailable.

On my machine jq depends on oniguruma. Both node and onigurama should be easy to install from your distro package manager.

Usually the problem with dependencies it not that they are hard to install but that you have something additional to install on a machine where you want to use it. Sometimes you might not even have the permission to do so.

Re: FX: An interactive alternative to jq to process JSON

#22
post #3

Apparently, the author wrote this tool because jid was struggling with a 7MB JSON file. See https://github.com/simeji/jid/issues/66#issuecomment-4436718...

A generic (partial) solution to this type of thing is just to sample a number of lines from the large input, and do the investigation on that. shuf -n 1000 file This is part of coreutils. There's also jiq, which is a clone of jid (mentioned elsewhere) but with jq syntax

That would only work if it's line separated JSON though. If you cut off the first 1000 lines of a big JSON file it will be invalid.

Re: FX: An interactive alternative to jq to process JSON

#23
post #9

JQ syntax feels too unusual, doesn't resemble known code, gives me the feeling of looking into cryptic Perl or regex, could never remember the simplest things. For example how would you take key k1 from a list of dicts [{k1: v1, k2: v2}, {k1: v3}]?

I agree that jq's query language is very obtuse and probably my biggest barrier towards learning it. I have found great mileage using gron [1], which is very different from jq, but its goal is to promote exploration of a JSON file through common unix tools such as awk and grep. 1: https://github.com/tomnomnom/gron

> I have found great mileage using gron

`gron` is great but doesn't seem to handle some (extreme-ish) situations that `jq` can, e.g. the json output from the fastnbt-tools. You either get a `token too long` error using `gron -s` because the input is too long (it's 90MB, that's fair) or you get only one set of outputs per key (iyswim) because they get overlapped in memory.

Re: FX: An interactive alternative to jq to process JSON

#24
post #22

Earlier quoted context omitted.

A generic (partial) solution to this type of thing is just to sample a number of lines from the large input, and do the investigation on that. shuf -n 1000 file This is part of coreutils. There's also jiq, which is a clone of jid (mentioned elsewhere) but with jq syntax

That would only work if it's line separated JSON though. If you cut off the first 1000 lines of a big JSON file it will be invalid.

Then, use

  jq —-compact-output '.' | head -10 | foo
That is also useful for grepping to filter on records of interest.

jq also has --stream for handling large inputs.

Re: FX: An interactive alternative to jq to process JSON

#25
post #9

JQ syntax feels too unusual, doesn't resemble known code, gives me the feeling of looking into cryptic Perl or regex, could never remember the simplest things. For example how would you take key k1 from a list of dicts [{k1: v1, k2: v2}, {k1: v3}]?

Had the same experience. That's why I've written jql[0], which puts a uniform lispy spin on CLI JSON processing. I now use it almost exclusively instead of jq. Check it out if you're looking for alternatives. And by the way, you can achieve live preview with any of these CLI tools by using fzf. This is the snippet for jql for example: `echo '' | fzf --print-query --preview-window wrap --preview 'cat test.json | jql {…

`jql` looks interesting - is there an easy way to do the equivalent of `jq`'s `to_entries[]`? (e.g. turns `{"x":"y"}{"a":"b"}` into `{"key":"x","value":"y"}{"key":"a","value":"b"}` which I've needed a lot recently for dealing with output with unknown keys.)

Re: FX: An interactive alternative to jq to process JSON

#26
post #9

JQ syntax feels too unusual, doesn't resemble known code, gives me the feeling of looking into cryptic Perl or regex, could never remember the simplest things. For example how would you take key k1 from a list of dicts [{k1: v1, k2: v2}, {k1: v3}]?

> For example how would you take key k1 from a list of dicts [{k1: v1, k2: v2}, {k1: v3}]?

    '.[].k1'

Re: FX: An interactive alternative to jq to process JSON

#27

Earlier quoted context omitted.

I agree that jq's query language is very obtuse and probably my biggest barrier towards learning it. I have found great mileage using gron [1], which is very different from jq, but its goal is to promote exploration of a JSON file through common unix tools such as awk and grep. 1: https://github.com/tomnomnom/gron

> I have found great mileage using gron `gron` is great but doesn't seem to handle some (extreme-ish) situations that `jq` can, e.g. the json output from the fastnbt-tools. You either get a `token too long` error using `gron -s` because the input is too long (it's 90MB, that's fair) or you get only one set of outputs per key (iyswim) because they get overlapped in memory.

> or you get only one set of outputs per key (iyswim) because they get overlapped in memory

That sounds like a major bug. So it will silently skip data that you wanted?

Re: FX: An interactive alternative to jq to process JSON

#28
post #9

JQ syntax feels too unusual, doesn't resemble known code, gives me the feeling of looking into cryptic Perl or regex, could never remember the simplest things. For example how would you take key k1 from a list of dicts [{k1: v1, k2: v2}, {k1: v3}]?

  ╰─$ echo '[{"k1": "v1", "k2": "v2"}, {"k1": "v3"}]' | jq -r '.[] | .k1'
  v1
  v3
https://codefaster.substack.com/p/mastering-jq-part-1-59c

1. parse a json value from stdin and set it as the initial result

2. for each function, apply the function to the result, and set the output as the result for the next function.

3. The final result is pretty printed on stdout.

Re: FX: An interactive alternative to jq to process JSON

#29
post #9

JQ syntax feels too unusual, doesn't resemble known code, gives me the feeling of looking into cryptic Perl or regex, could never remember the simplest things. For example how would you take key k1 from a list of dicts [{k1: v1, k2: v2}, {k1: v3}]?

> For example how would you take key k1 from a list of dicts [{k1: v1, k2: v2}, {k1: v3}]?

Do you mean something like:

    .[].k1
Give it a try.

https://jqplay.org/

jq does have a learning curve, but just like any query language, including SQL, first you need to learn the basics of the query language in order to get things to work.

In this case:

* you know that .[] iterates over objects, so you use it to unpack the root array,

* you know you get a stream of objects, thus from those you use the .k1 filter to get the values of each k1 key.

Here's jq's manual on basic filters: https://stedolan.github.io/jq/manual/#Basicfilters

After you get jq to filter out what you want, you can work on getting it to output results in whatever format you wish.

Re: FX: An interactive alternative to jq to process JSON

#30
post #9

JQ syntax feels too unusual, doesn't resemble known code, gives me the feeling of looking into cryptic Perl or regex, could never remember the simplest things. For example how would you take key k1 from a list of dicts [{k1: v1, k2: v2}, {k1: v3}]?

I've struggled with the jq language when doing complicated things, but generally felt it was just the problem that was tricky. Generally I feel like I'm learning an actual useful language, though I guess Perl, Regex fall into that same category, what seems impenetreble at first later becomes almost second nature as you use $ to mean end of line in vi and so on. Then if you don't do it for a while, you forget the more obscure bits.

My approach to the example would be to use `.[] | .k1` which I think does what you want, and like bash command line pipes, you can build up to it semi-interactively.

The bits I struggle with JQ often involve irregular json, where a value might be missing, or null, or a list, not sure what the idiomatic way to deal with that is if there is one.

Post reply on HN