Jq: Can't have period in key

Created on 31 Oct 2013  路  6Comments  路  Source: stedolan/jq

Hi! My input looks like this:

{
"results" : [
{
"some.key" : 3
},
{
"some.key" : 4
}
]
}

I'd like to be able to do this
./jq ".results.some.key" input

But I can't. I can't figure out how to access those keys, is this possible? Also, I cannot change the schema....

support

Most helpful comment

Sorry, I could't find it in the documentation but I found it in a closed issue:

./jq '.results[]["some.key"]' input

All 6 comments

Sorry, I could't find it in the documentation but I found it in a closed issue:

./jq '.results[]["some.key"]' input

Yeah, the docs are a bit weak on this point. .foo is just shorthand for .["foo"], like in JS (or lua).

I ran into this, and in case it helps anyone:

  • .foo is equivalent to .["foo"] (as noted above)
  • .foo.bar is equivalent to any of:

    • .["foo"]["bar"]

    • .foo["bar"]

    • .["foo"].bar

Sorry for the bump. What about the case where you want to interpolate a key with a dot? i.e.

$ echo '[{"foo":2},{"foo":1}]' | jq -r '.[] | "\(.foo)"'
2
1
$ echo '[{"foo.bar":2},{"foo.bar":1}]' | jq -r '.[] | "\(.what do I put here???)"'

@marianogappa For all non-ident-like keys use .["whatever the key is goes here"]. Interpolation uses jq expressions, so

$ echo '[{"foo.bar":2},{"foo.bar":1}]' | jq -r '.[] | "\(.["foo.bar"])"'

I think you can more with terraform output -json directly like this, without the need of a third party python library.

$ terraform output -json | jq -r ".quadrant.value.\"$QUAD.region\""
us-east-1
Was this page helpful?
0 / 5 - 0 ratings

Related issues

kelchy picture kelchy  路  4Comments

neowulf picture neowulf  路  3Comments

kaihendry picture kaihendry  路  4Comments

rubensayshi picture rubensayshi  路  3Comments

thedward picture thedward  路  3Comments