'Concise notation for JQ

According to jq tutorial, end of §4.1, there is a compact notation for jq where the | operator (and the spaces surrounding it) can just be skipped.

On the SQuAD 2.0 devset (download link), the following expressions work:

$> cat src/dev-v2.0.json | jq ".data" | jq ".[]" | jq ".paragraphs" | jq ".[]" | jq ".qas" | jq ".[]" | jq ".question" | head
$> cat src/dev-v2.0.json | jq ".data | .[] | .paragraphs | .[] | .qas | .[] | .question" | head

But the "concise" notation doesn't:

$> cat src/dev-v2.0.json | jq ".data.[].paragraphs.[].qas.[].question" | head

jq: error: syntax error, unexpected '[', expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:jq: error: syntax error, unexpected '[', expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
.data.[].paragraphs.[].qas.[].question      
jq: 1 compile error

As the error hints, this may be an encoding issue, but with the quotes around the expression, this seems unlikely. I thus follow the jq recommendation to ask that at StackOverflow: What am I missing here?

I am using Ubuntu 20.04.3 LTS, jq-1.6, bash 5.0.17(1).

jq


Solution 1:[1]

  • .[] accesses the members of the array (or object) in the context ..
  • .data | .[] accesses a field called data of the object in the context .. By piping it into the next filter, the accessed field becomes the new context ., so .[] accesses the members of the array (or object) in that context.
  • .data[] accesses the members of the array (or object) in a field called data of the object in the context ..
jq ".data[].paragraphs[].qas[].question" dev-v2.0.json | head
"In what country is Normandy located?"
"When were the Normans in Normandy?"
"From which countries did the Norse originate?"
"Who was the Norse leader?"
"What century did the Normans first gain their separate identity?"
"Who gave their name to Normandy in the 1000's and 1100's"
"What is France a region of?"
"Who did King Charles III swear fealty to?"
"When did the Frankish identity emerge?"
"Who was the duke in the battle of Hastings?"

Solution 2:[2]

The valid abbreviated form of .x|.[]|.y is .x[].y

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 ikegami