still_learning
still_learning

Reputation: 35

how to print all key and one of the values from json using jq

I have a test.json file and I want to print:

prob1, 9
prob2, 10
prob3, 11

cat test.json | jq --raw-output '.[].abc'

return 9,10,11 but I am not sure how to print keys as well.

{
   "prob1":{
      "abc":9,
      "abcd":2,
      "Foo":3
   },
   "prob2":{
      "abc":10,
      "abcd":2,
      "Foo":3
   },
   "prob3":{
      "abc":11,
      "abcd":2,
      "Foo":3
   }
}

Upvotes: 0

Views: 618

Answers (2)

ikegami
ikegami

Reputation: 385546

Two approaches:

jq -r 'to_entries[] | "\( .key ): \( .value.abc )"'

Demo on jqplay

jq -r 'keys[] as $key | .[$key] | "\( $key ): \( .abc )"'

Demo on jqplay

Upvotes: 1

still_learning
still_learning

Reputation: 35

jq -r 'to_entries[] | "\(.key): \(.value.abc)"'
prob1: 9
prob2: 10
prob3: 11

Upvotes: 1

Related Questions