Any idea why the 2nd case not working in jq command?
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
Solution 1:
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"]'