I am trying to fetch message field from below json file in comma separated value
[
{
"title":"second bad commit",
"message":"second bad commit"
},
{
"title":"first bad commit",
"message":"first bad commit",
}
]
My attempt to do that with jq: jq -r '.message | join(",")'
It keeps throwing following error:
jq: error (at <stdin>:0): Cannot index array with string "message"
So, how can I make jq output comma separated when key is absent
CodePudding user response:
Assuming that your data is actually:
[
{
"title":"second bad commit",
"message":"second bad commit"
},
{
"title":"first bad commit",
"message":"first bad commit"
}
]
Then you'll need to use map to map each value:
$ jq 'map(.message)'
[
"second bad commit",
"first bad commit"
]
And then just the join pipe:
$ jq 'map(.message) | join(",")'
"second bad commit,first bad commit"
And then adding -r to output the raw value.:
$ jq -r 'map(.message) | join(",")'
second bad commit,first bad commit
