3

My program returns JSON into stdout and it is an object with operations field, which contains a list of operations. Each operation contains fields including id fields, for example:

{
    "operations": [
        {
            "id": "694ef895-793f4631-41103e8-8ab59e66",

I am trying to get a list of ids from this with jq.

If I am writing

jq '.operations | .[] | .id'

it works, and if I am wiritng

jq '.operations | .[].id'

it works too.

I.e. juxtaposition is the same as | here.

But if I write

jq '.operations.[].id'

it fails with message

jq: error: syntax error, unexpected '[', expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at , line 1: .operations.[].id jq: 1 compile error

why and how to fix?

0

1 Answer 1

6

As long as you are only comparing .foo.bar and |, there's no difference, as the man-page says under "BASIC FILTERS":

A filter of the form .foo.bar is equivalent to .foo|.bar.

And yes, you need .operations[].id in your case.

1
  • For anyone wondering about the last example command in the question, or a similar error, note that there's no . between .operations and the following [] ('index all') filter. Commented Jul 23, 2021 at 18:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .