Reputation: 646
Suppose I have the following json in the file ver.json:
[{"id":"123","product":13038},{"id":"456","product":1212}]
Why when I apply the "jq" command I get this result?
jq -r '(.[].id|tostring) + "," + (.[].product|tostring)' ver.json
123,13038
456,13038
123,1212
456,1212
This would be the correct answer:
123,13038
456,1212
Upvotes: 0
Views: 96
Reputation: 116670
Or more compactly:
$ jq -r '.[] | "\(.id),\(.product)"'
123,13038
456,1212
It may also be worth noting that @csv
retains the types:
$ jq -r '.[] | [.id,.product] | @csv'
"123",13038
"456",1212
Upvotes: 2
Reputation: 50019
This is a little messy. Because you are piping twice and concatenating you are ending up with a cartesian product.
Instead pipe .[]
once to two different parentheticals:
~$ jq -r '.[] | (.id | tostring) + "," + (.product | tostring)' ver.json
123,13038
456,1212
Upvotes: 4