{
"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
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/
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.
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-1.5
{
"pragma": "no-cache",
"server": "tsa_f",
"status": "200 OK",
"x_response_time": "438",
"x-access-level": "read-write"
}
"no-cache"
jq: error: access/0 is not defined at
.x-access-level
jq: error: level/0 is not defined at
.x-access-level
jq: 2 compile errors
jq: error: access/0 is not defined at
.x-access-level
jq: error: level/0 is not defined at
.x-access-level
jq: 2 compile errors
jq: error: x/0 is not defined at
.[x-access-level]
jq: error: access/0 is not defined at
.[x-access-level]
jq: error: level/0 is not defined at
.[x-access-level]
jq: 3 compile errors
"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.jsonthe double quotes are consumed by your shell, so
jqsees 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.
Most helpful comment
@RedactedCode
When you type:
the double quotes are consumed by your shell, so
jqsees your expression as:In order to preserve the double quotes in the string passed to
jq, try instead: