Jq: Cannot index object with number

Created on 3 Mar 2018  路  6Comments  路  Source: stedolan/jq

curl 'url' | jq '.[0]'
https://pastebin.com/5i5VDBYz
returns
jq: error (at <stdin>:0): Cannot index object with number
on Linux box.example.com 4.14.17-x86_64 (Ubuntu 16.04)

support

Most helpful comment

Here is one approach for anyone finding this on their search for an answer:

echo '[{"name":"joe"}, {"name":"john"}, {"name":"jose"}]' | jq '[.[] | .name] | .[1]'

This will wrap the processed data in an array from which you can index a particular value.

All 6 comments

The JSON at https://pastebin.com/5i5VDBYz is an object, and as the error message says, you "cannot index [an] object with [a] number". Since you haven't indicated what you are trying to do, it's hard to know what you intended, but you might like to lookup .[] in the jq manual.

For future reference, please ask usage questions at stackoverflow.com using the jq tag. See also the "minimal complete verifiable example" guidelines at http://stackoverflow.com/help/mcve

thx. i am trying to extract a single element from the JSON.

If you cannot figure it out after reading the (very brief) tutorial and/or scanning the manual, I'd suggest posting a question at stackoverflow.com with the jq tag, following the previously-mentioned mcve guidelines.

Here is one approach for anyone finding this on their search for an answer:

echo '[{"name":"joe"}, {"name":"john"}, {"name":"jose"}]' | jq '[.[] | .name] | .[1]'

This will wrap the processed data in an array from which you can index a particular value.

Here is one approach for anyone finding this on their search for an answer:

echo '[{"name":"joe"}, {"name":"john"}, {"name":"jose"}]' | jq '[.[] | .name] | .[1]'

This will wrap the processed data in an array from which you can index a particular value.

Thank you so much @rbrisita

echo '[{"name":"joe"}, {"name":"john"}, {"name":"jose"}]' | jq '[.[] ] | .[0]'
will bring back the first element of any json doc AFAIK

Was this page helpful?
0 / 5 - 0 ratings