Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

select(.key="value") emits even non-matching items

I have the following test.json

{"type":"RECORD","record":{"key_A": "apples","key_B":"bananas"}}
{"type":"RECORD","record":{"key_A": "apples","key_B":"oranges"}}

Then see the following command and output.

cat test.json | jq 'select(.record.key_B="oranges")' 
{
  "type": "RECORD",
  "record": {
    "key_A": "apples",
    "key_B": "bananas"
  }
}
{
  "type": "RECORD",
  "record": {
    "key_A": "apples",
    "key_B": "oranges"
  }
}

I wish it would only output the record which match the select – the ones where .record.key_B = oranges so only the first record. How can I do this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

= is an assignment operator. == is an equality-comparison operator.

You want select(.record.key_B == "oranges").

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading