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

jq: filter array and project other field

Here my document:

[
  {
    "id": "9f0e27fe-3b8f-4857-8e1d-e57e7a3f4c31",
    "identifier": [
      {
        "system": {
          "value": "urn:oid:1.3.6.1.4.1.19126.3"
        },
        "value": {
          "value": "Y3454867M"
        }
      },
      {
        "system": {
          "value": "urn:oid:2.16.724.4.9.10.2"
        },
        "value": {
          "value": "108505134"
        }
      }
    ]
  }
]

I need to pick only .identifier[where .system.value == "urn:oid:1.3.6.1.4.1.19126.3"] and project .identifier.value.value.

Desired output:

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

[
  {
    "id": "9f0e27fe-3b8f-4857-8e1d-e57e7a3f4c31",
    "identifier": "Y3454867M"
  }
]

I’ve been playing with map and select but I don’t quite figure out what’s the right way to get it.

Any ideas?

>Solution :

This approach uses first to get the first result, in case there is more than one array item matching the criteria.

jq --arg v "urn:oid:1.3.6.1.4.1.19126.3" '
  map(.identifier |= first(.[] | select(.system.value == $v).value.value))
'
[
  {
    "id": "9f0e27fe-3b8f-4857-8e1d-e57e7a3f4c31",
    "identifier": "Y3454867M"
  }
]

Demo

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