3

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_ids 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 ?

SebMa
  • 4,037
  • 29
  • 39

1 Answers1

3

If your jq has inputs then it would be best to use it in conjunction with the -n command-line option and the following jq filter:

first(inputs | select(.format_id =="18"))

If your jq does not have inputs

... then you'd have to use the -s command-line option, e.g. with the following filter:

first(.[] | select(.format_id =="18"))

Here, using inputs is preferable as it requires less RAM.

peak
  • 105,803
  • 17
  • 152
  • 177
  • Thanks a lot ! I'd tried this `first(select(.format_id =="18"))` but it didn't work. I wish I understood `jq` a little more. I'd read the official tutorial and manual but it does not say much about `inputs` (only two lines of text without a `inputs` use case example). There are many tutorials on the internet. I really don't know which to read. I'd read some but none that really gave me a better `jg` understanding when it comes to deal with dictionary only data. Where can I find "very good" `jq` tutorial according to you ? – SebMa Apr 24 '20 at 19:32
  • @SebMa You ran it like `jq -n 'first(select(.format_id =="18"))' file.json`, right? – oguz ismail Apr 24 '20 at 19:38
  • 1
    SebMa - As it happens, the introduction I wrote some time ago does have a whole section devoted to `inputs`: https://github.com/pkoppstein/jq/wiki/A-Stream-oriented-Introduction-to-jq#on-the-importance-of-inputs – peak Apr 24 '20 at 20:07
  • @oguzismail Nope, I didn't use `jq -n` but `jq` – SebMa Apr 24 '20 at 20:30
  • @peak Thanks again for your help. Can you please update your answer by prefixing `...| jq -n '` to your first command and `...| jq -s '` to the second so that it would be more explicit for other `jq` rookies like me :) BTW : Can you also add your `inputs` documentation link to your answer ? But if you don't have the time, please tell me and I'll do it right away. – SebMa Apr 25 '20 at 12:24