0

I have the following JSON file list.json :

[
  {
    "$type": "Profile",
    "ReferenceId": "6e374d9e-5bbe-4015-acd7-ae6f04de0db5",
    "Name": " aaa"
  },
  {
    "$type": "Profile",
    "ReferenceId": "5c055f82-696b-48c2-9b7f-a540d48fcd19",
    "Name": " bbb"
  }
 ]

In bash, I would like to extract in variable the value of the first occurance of the key "ReferenceId" where the key "Name" equal "bbb".

I tried jq -r ".[] | select(.Name==\"bbb?take=300") | .ReferenceId" < list.json but nothing works. I suppose i need to use --slurpfile but I don't see how to do that.

Thx in advance for your replies.

  • The file example you give and the search criteria do not match up. In fact, there is even a syntax error in the command. When changing examples, please make extra sure they are still a valid representation of your problem. – Daniel B Oct 31 '22 at 16:12

1 Answers1

0

Ignoring the broken JSON (no comma allowed on the last key:value pair) and that the Name does not equal "bbb" (there's a space):

jq -r 'map(select(.Name == " bbb")) | first | .ReferenceId' list.json

If you want "the name contains bbb" then select(.Name | contains("bbb"))

glenn jackman
  • 25,463
  • 6
  • 46
  • 69