Reputation: 767
I thought I had done a lot on jq command. But why the following 2 command, one is working, and the other fails ?
Working case:
echo '{"destinations": {"results": [{"sessions": "16903"}]}}' | jq '.destinations'
{
"results": [
{
"sessions": "16903"
}
]
}
Broken Case:
echo '{"pre-destinations": {"results": [{"sessions": "16903"}]}}' | jq '.pre-destinations'
jq: error: destinations/0 is not defined at <top-level>, line 1:
.pre-destinations
jq: 1 compile error
The root cause is I was using a '-' in the key value, but why it fails ?
Jack
Upvotes: 0
Views: 208
Reputation: 3711
The accepted answer doesn't work for any version of jq I've tried. This is the only way I could get it to work:
echo '{"pre-destinations": {"results": [{"sessions": "16903"}]}}' | \
jq '. | to_entries | .[] | select (.key == "pre-destinations") | .value'
Upvotes: 0
Reputation: 116740
In the key named ".pre-destinations", the hyphen is regarded as a special character, so you would have to quote the key name, e.g.
jq '".pre-destinations"'
or more robustly (with respect to variations between different versions of jq):
jq '["pre-destinations"]'
Upvotes: 2