Jq: issue accessing keys which contain - (hyphen) in the name

Created on 23 Apr 2018  路  6Comments  路  Source: stedolan/jq

{
"1": 4,
"2": 0,
"651": 3,
"kunal-test": 3,
"abc" : 14
}

Imagine a JSON like this.
Uisng jq ."kunal-test"

results in following error:
jq: 1 compile error
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 109 100 109 0 0 243 0 --:--:-- --:--:-- --:--:-- 244
(23) Failed writing body

support

Most helpful comment

@RedactedCode

When you type:

jq .["x-access-level"] fragment.json

the double quotes are consumed by your shell, so jq sees your expression as:

.[x-access-level]

In order to preserve the double quotes in the string passed to jq, try instead:

jq '.["x-access-level"]' fragment.json

All 6 comments

  1. On a Mac or *ix machine, you would have to enclose the filter as shown in single-quotes; you鈥檇 have to perform some gymnastics if using Windows unless you put the filter in a file as explained in the online jq manual: https://stedolan.github.io/jq/manual/

  2. The error message you report does not correspond at all with the other details given. In future, please follow the guidelines at https://stackoverflow.com/help/mcve as much as possible.

  3. For future reference, please post usage questions at stackoverflow.com using the jq tag: https://stackoverflow.com/questions/tagged/jq

It does seem there may be a problem with jq and hyphenated key names.
The json specification doesn't exclude hyphens in key names.

Please note the following.

jq 32-bit, Linux

jq --version

jq-1.5

jq . fragment.json

{
"pragma": "no-cache",
"server": "tsa_f",
"status": "200 OK",
"x_response_time": "438",
"x-access-level": "read-write"
}

jq .pragma fragment.json

"no-cache"

jq .x-access-level fragment.json

jq: error: access/0 is not defined at , line 1:
.x-access-level
jq: error: level/0 is not defined at , line 1:
.x-access-level
jq: 2 compile errors

jq ."x-access-level" fragment.json

jq: error: access/0 is not defined at , line 1:
.x-access-level
jq: error: level/0 is not defined at , line 1:
.x-access-level
jq: 2 compile errors

jq .["x-access-level"] fragment.json

jq: error: x/0 is not defined at , line 1:
.[x-access-level]
jq: error: access/0 is not defined at , line 1:
.[x-access-level]
jq: error: level/0 is not defined at , line 1:
.[x-access-level]
jq: 3 compile errors

jq .x_response_time fragment.json

"438"

Thus there does not seem to be any syntactical device that permits jq to correctly return values of keys that contain hyphens.

As documented in many places, you can always use the fundamental method for accessing the value of a key: .["KEY"]

There are alternatives depending on your version of jq, but the fundamental method is supported by all versions.

@RedactedCode

When you type:

jq .["x-access-level"] fragment.json

the double quotes are consumed by your shell, so jq sees your expression as:

.[x-access-level]

In order to preserve the double quotes in the string passed to jq, try instead:

jq '.["x-access-level"]' fragment.json

@RedactedCode

When you type:

jq .["x-access-level"] fragment.json

the double quotes are consumed by your shell, so jq sees your expression as:

.[x-access-level]

In order to preserve the double quotes in the string passed to jq, try instead:

jq '.["x-access-level"]' fragment.json

This way does not work with variables expansion (zsh, Ubuntu 18.04) - how can we handle this case?

$ zsh --version
zsh 5.4.2 (x86_64-ubuntu-linux-gnu)
$ json='{"cloud1": {"level1": {"apps-owners": "owner1 owner2 owner3"}}}'
$ cloud=cloud1
$ level=level1
$ echo ${json} |  jq -r -c '.${cloud}.${level}.["apps-owners"]'
jq: error: syntax error, unexpected '$' (Unix shell quoting issues?) at <top-level>, line 1:
.${cloud}.${level}.["apps-owners"] 
jq: error: try .["field"] instead of .field for unusually named fields at <top-level>, line 1:
.${cloud}.${level}.["apps-owners"]
jq: 2 compile errors
$ echo ${json} |  jq -r -c ".${cloud}.${level}.[\"apps-owners\"]"  
jq: error: syntax error, unexpected '[', expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
.cloud1.level1.["apps-owners"]               
jq: 1 compile error
$ echo ${json} |  jq -r -c ".${cloud}.${level}.\[\"apps-owners\"\]"
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
.cloud1.level1.\["apps-owners"\]               
jq: 1 compile error
$ echo ${json} |  jq -r -c '.${cloud}.${level}.["apps-owners"]'
jq: error: syntax error, unexpected '$' (Unix shell quoting issues?) at <top-level>, line 1:
.${cloud}.${level}.["apps-owners"] 
jq: error: try .["field"] instead of .field for unusually named fields at <top-level>, line 1:
.${cloud}.${level}.["apps-owners"]
jq: 2 compile errors

@MaurGi There are command-line options for that, like --arg varname value and so on.

Was this page helpful?
0 / 5 - 0 ratings