Kyle Banerjee
Kyle Banerjee

Reputation: 2824

Conditional jq extraction of two elements in different places of hierarchy

I have a json document that I need to extract both the value of subfield a of tags 035 and 999 when 035.subfields[].ind1 and 035.subfields[].ind2 are both "0". Here is my document:

{
  "leader": "01815cam a22004934a 4500",
  "fields": [
    {
      "001": "mig00005597434"
    },
    {
      "035": {
        "subfields": [
          {
            "a": "(OCoLC)67764532"
          }
        ],
        "ind1": " ",
        "ind2": " "
      }
    },
    {
      "035": {
        "subfields": [
          {
            "a": "ocm00000001projmusemuse13792"
          }
        ],
        "ind1": "0",
        "ind2": "0"
      }
    },
    {
      "245": {
        "subfields": [
          {
            "a": "The Sins of the Father"
          },
          {
            "b": "A Romance of the South /"
          },
          {
            "c": "by Thomas Dixon ; with an introduction by Steven Weisenburger."
          }
        ],
        "ind1": "1",
        "ind2": "4"
      }
    },
    {
      "999": {
        "subfields": [
          {
            "i": "a1b7fa7e-9e2e-4973-96d0-3939235c8b80"
          },
          {
            "s": "020d182f-ba5d-4bbe-a2eb-e4390aa0736c"
          }
        ],
        "ind1": "f",
        "ind2": "f"
      }
    }
  ]
}

My desired output is

ocm00000001projmusemuse13792     020d182f-ba5d-4bbe-a2eb-e4390aa0736c

When I try to get both values,

jq -r '.fields[] | [(select(."035".ind1 == "0" and ."035".ind2 == "0") | ."035".subfields[].a), (select(."999") |."999".subfields[].s)] '

I get

[]
[]
[
  "ocm00000001projmusemuse13792"
]
[]
[
  null,
  "020d182f-ba5d-4bbe-a2eb-e4390aa0736c"
]

But and other variations where I try to move the 999 selection (e.g.

jq '.fields[] | [(select(."035".ind1 == "0" and ."035".ind2 == "0") | ."035".subfields[].a), (select(."999".subfields[].s) | ."999".subfields[].s)] '

I get that I can't iterate over null. What am I missing?

Upvotes: 0

Views: 125

Answers (1)

peak
peak

Reputation: 117027

With the -r command-line option, the filter:

[.fields[]
 | (select(."035".ind1 == "0" and ."035".ind2 == "0")
    | (."035".subfields[] | select(.a) | .a) ),
   (select(."999")
    | (."999".subfields[] | select(.s) | .s) ) ]
| join(" ")

yields:

ocm00000001projmusemuse13792 020d182f-ba5d-4bbe-a2eb-e4390aa0736c

Upvotes: 1

Related Questions