-1

I have this json object like this :

 [ {
    "name": "ACCOUNT-V1",
    "version": "1.3.0"
  },
  {
    "name": "IDENTIFIER-V1",
    "version": "1.1.0"
  },
  {
    "name": "LOCATION-V1",
    "version": "1.6.0"
  }
]

I'd like to parse and print like this

ACCOUNT-V1 1.3.0
IDENTIFIER-V1 1.1.0
LOCATION-V1 1.6.0

tried with

cat json_content.json | jq ' .[] | .name .version'

just getting empty array [] in output

Venu S
  • 3,251
  • 1
  • 9
  • 25
  • 1
    Show us what you have tried with jq, and then explain what it's not doing right. – Andy Lester Aug 11 '21 at 15:36
  • The printed output spec is unclear. The first field is delimited by 2 spaces and the second two are delimited by 1 space. Regardless, you probably want to start by extracting the two fields and joining them: `.[] | "\(.name) \(.version)"`, yes? (you can use `-r` to remove the quotes in the output) – ggorlen Aug 11 '21 at 15:37
  • thanks @ggorlen, it worked, I would like to accept your answer – Venu S Aug 11 '21 at 15:42
  • Seems like a dupe of [Concat 2 fields in JSON using jq](https://stackoverflow.com/a/45527623/6243352) – ggorlen Aug 11 '21 at 15:44

2 Answers2

1
jq --raw-output '.[] | "\(.name) \(.version)"'

Where

  • .[] Loops over the array

  • "\(.name) \(.version)" creates a string with name and version key
    Using string interpolation

  • --raw-putput ensures no quotes on the output


Try it online!

0stone0
  • 34,288
  • 4
  • 39
  • 64
0

In JSON :

  • [ .. ] is for array
  • { .. } is object

So if you put your json to a variable like

const jsx = [ {
    "name": "ACCOUNT-V1",
    "version": "1.3.0"
  },
  {
    "name": "IDENTIFIER-V1",
    "version": "1.1.0"
  },
  {
    "name": "LOCATION-V1",
    "version": "1.6.0"
  }
];

It will be :

  • jsx[0].name = "ACCOUNT-V1"
  • jsx[2].version = "1.6.0"
  • and so on