Live data from Hacker News

FX: An interactive alternative to jq to process JSON

github.com

31–40 of 65 posts

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

#33
post #31

gron [0] is another interesting JSON processor that follows UNIX philosophy: [0] https://github.com/tomnomnom/gron "Make JSON greppable!" "gron transforms JSON into discrete assignments to make it easier to grep for what you want and see the absolute 'path' to it."

I absolutely love gron, but I have to confess I feel dirty when I use it. It's unashamedly a brute-force tool in a world with plenty of elegant alternatives, and the main reason to use it is pure laziness to just shamelessly grep stuff around. And I love it for that.

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

#35
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 {…

Does fzf let you specify autocomplete for the command based on the input? That would be amazing.

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

#36

Earlier quoted context omitted.

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.)

For the general case of multiple keys and values - no. It sounds reasonable, though, so I'll think about whether to add an entries function or a map function that would allow doing this in a simple way.

For the special case you wrote as an example, where each object is just a single key-value, it's possible:

  (object
      "key" (pipe (keys) (0))
      "value" (pipe ((keys)) (0)))

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

#38

Earlier quoted context omitted.

> 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?

> That sounds like a major bug.

It's definitely an oddness when you have multiple objects at the same level that aren't in an array but I guess the explanation there is "they should all be on their own individual lines as streaming json" which `gron` does handle correctly.

    (echo '{"a":"23"}'; echo '{"a":"25"}') | gron -s
    json = [];
    json[0] = {};
    json[0].a = "23";
    json[1] = {};
    json[1].a = "25";
> So it will silently skip data that you wanted?

Yeah.

    echo '{"a":"23"}{"a":"25"}' | gron
    json = {};
    json.a = "23";
The `-s` option doesn't help.

    echo '{"a":"23"}{"a":"25"}' | gron -s
    json = [];
    json[0] = {};
    json[0].a = "23";

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

#39

Earlier quoted context omitted.

`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.)

For the general case of multiple keys and values - no. It sounds reasonable, though, so I'll think about whether to add an entries function or a map function that would allow doing this in a simple way. For the special case you wrote as an example, where each object is just a single key-value, it's possible: (object "key" (pipe (keys) (0)) "value" (pipe ((keys)) (0)))

> I'll think about whether to add an entries function or a map function that would allow doing this in a simple way.

That would be super, ta. `to_entries[]` is pretty much the major reason I've not managed to move off `jq` to anything else yet because it's just incredibly powerful in this situation.

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

#40

Earlier quoted context omitted.

For the general case of multiple keys and values - no. It sounds reasonable, though, so I'll think about whether to add an entries function or a map function that would allow doing this in a simple way. For the special case you wrote as an example, where each object is just a single key-value, it's possible: (object "key" (pipe (keys) (0)) "value" (pipe ((keys)) (0)))

> I'll think about whether to add an entries function or a map function that would allow doing this in a simple way. That would be super, ta. `to_entries[]` is pretty much the major reason I've not managed to move off `jq` to anything else yet because it's just incredibly powerful in this situation.

I've actually just gone ahead and added a way to do this - the zip function - in the v0.2.0 release.

The relevant jql snippet to solve this in the general case now is:

  (pipe
    (zip
      (keys)
      ((keys)))
    ((keys)
      (object
        "key" (0)
        "value" (1))))
It's not as terse as the jq equivalent - I'll probably add a way to create user-defined functions, so you can alias stuff like this to shorter forms - but that one will require more thought.
Post reply on HN