I have following json :
$ echo '{ "format_id": "250" }{ "format_id": "18" }{ "format_id": "140" }{ "format_id": "18" }{ "format_id": "244" }' | jq .
{
"format_id": "250"
}
{
"format_id": "18"
}
{
"format_id": "140"
}
{
"format_id": "18"
}
{
"format_id": "244"
}
I managed to extract the format_id
s for which it equals to "18" :
$ echo '{ "format_id": "250" }{ "format_id": "18" }{ "format_id": "140" }{ "format_id": "18" }{ "format_id": "244" }' | jq -r 'select(.format_id=="18")'
{
"format_id": "18"
}
{
"format_id": "18"
}
I want to extract out of that the 1st occurrence of format_id
.
So I tried the solutions given here but none of them worked because I guess they need to be adapted to my input data somehow :
$ echo '{ "format_id": "18" }{ "format_id": "18" }' | jq '[.[]|select(.format_id)][0]'
jq: error (at <stdin>:1): Cannot index string with string "format_id"
jq: error (at <stdin>:1): Cannot index string with string "format_id"
$ echo '{ "format_id": "18" }{ "format_id": "18" }' | jq '( map(select(.format_id)) | first )'
jq: error (at <stdin>:1): Cannot index string with string "format_id"
jq: error (at <stdin>:1): Cannot index string with string "format_id"
$ echo '{ "format_id": "18" }{ "format_id": "18" }' | jq '( first(.[] | select(.format_id)) )'
jq: error (at <stdin>:1): Cannot index string with string "format_id"
jq: error (at <stdin>:1): Cannot index string with string "format_id"
$ echo '{ "format_id": "18" }{ "format_id": "18" }' | jq 'map(select(.format_id))|.[0]'
jq: error (at <stdin>:1): Cannot index string with string "format_id"
jq: error (at <stdin>:1): Cannot index string with string "format_id"
Can you please help me ?