1

input json:

[
  {
    "name": "cust1",
    "grp": [
      {
        "id": "46",
        "name": "BA2"
      },
      {
        "id": "36",
        "name": "GA1"
      },
      {
        "id": "47",
        "name": "NA1"
      },
      {
        "id": "37",
        "name": "TR3"
      },
      {
        "id": "38",
        "name": "TS1"
      }
    ]
  }
]

how to get .grp[] names separated with space on @tsv output?

$ jq -r '.[]|[.name, (.grp//[{"name":"-"}]|.[]|" ",.name)]|@tsv' test_inp
cust1           BA2             GA1             NA1             TR3             TS1

expected output for above sample:

cust[TAB]BA2[SPACE]GA1[SPACE]NA1[SPACE]TR3[SPACE]TS1
2

jq approach:

jq -r '.[] | [.name, (.grp | map(.name) | join(" "))] | @tsv' input.json

The output:

cust1   BA2 GA1 NA1 TR3 TS1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.