Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

For whatever reason jq is one tool that I simply can never remember the syntax for. It's ChatGPT every time for me. I just can't remember the specifics of how it differs from jsonpath vs jmespath (used by AWS) .... I wish there was a way for every tool to just use jsonpath instead.


Gron( + grep) can also be a handy combination is this case.


+1 for gron. Breaking a JSON field like { "foo": { "bar": ["baz", "bop"]}} into a greppable stream like

foo.bar.[0] = baz

foo.bar.[1] = bop

and then copying and pasting the results into jq makes the whole iteration loop much much tighter.


Not a 1:1 replacement, but I created https://github.com/pacha/cels because I wanted to have a more intuitive way of working with JSON and YAML files


JSONpath and jmespath are objectively worse than the jq language though, as you can only query, but jq also allows for very powerful transformations.

As for learning it, it's the same with any tool, key is repetition and regular use.


i get that it's more powerful but I almost consider it an anti-feature. I have very good tools for doing transformations (sed,awk,perl, etc etc) the problem is they all want line oriented format and JSON breaks that. So all I want is a tool to go from json => line oriented and I will do the rest with the vast library of experience I already have at transformations on the command line.


> So all I want is a tool to go from json => line oriented and I will do the rest with the vast library of experience I already have at transformations on the command line.*

The tool for that is likely https://github.com/tomnomnom/gron It's probably the best tool to go back and forth easily between json and line oriented.


`jq -c paths` will show you all the paths in a JSON text. You can totally use jq in a very gron-like manner. Try it.


We need to write a new tutorial.

The first and foremost thing to know about jq is that it's built on path expressions, so the first thing to learn is how to write path expressions. Fortunately path expressions are easy in jq!

  .a    # Get the value of the "a" key
        # in the current input object

  .[0]  # Get the value of the first
        # element in the current input
        # array

  .a[0] # Get the value of the first
        # element in the array at the
        # key named "a" in the current
        # input object.
        #
        # I.e., path expressions chain:

  .a[0].b # Get the value of the "b"
          # key in ...
Things get more interesting when you see that `.[]` is the iterator operator, and that you can use it in path expressions.

Things get really interesting when you see that `select(conditional expression)` can be used in path expressions joined with `|`.

Just this can be very useful. It's also useful to know about the magic `path()` function, and `paths`, which I often use to just list all the paths in an input JSON text. Try applying `jq -c paths` to a `kubectl get -o json pods` command's output!


This is my main usage of ChatGPT :-)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: