Pair value with all products of a filter

Viewed 61

I'm new to jq and I have a json response from a get request that looks like:

[
  {
    "vs": {
      "name": "vs_name",
      "pool": {
        "p_id_name": "XYZ",
        "members": [
          {
            "m_name": "XXX1",
            "id_name": "YYY1",
            "address": "ZZZ1"
          },
          {
            "m_name": "XXX2",
            "id_name": "YYY2",
            "address": "ZZZ2"
          }
        ]
      }
    }
  }
]

I'm trying to get an output that looks like (repating the p_id_name for each m_name):

XYZ, XXX1
XYZ, XXX2

I tried the following but it didn't work.

$ jq '.[].vs.pool|[.members[].m_name,.p_id_name]' file
[
  "XXX1",
  "XXX2",
  "XYZ"
]
1 Answers

Between square brackets, all products are collected into a single array. String interpolation doesn't have this effect.

.[].vs.pool | "\(.p_id_name), \(.members[].m_name)"

Online demo

If you want to output arrays, you need to create a separate array for each m_name.

.[].vs.pool | [.p_id_name] + (.members[] | [.m_name])

Online demo

Related