I am trying parse the value of a json property whose key has hyphens in it.
I read the json content into a variable using sub shell commands
srcJson=$(someshellcommand)
I pipe the variable into jq read operation (and also pipe it further to sed for some changes - but beside the point)
value=$(echo $srcJson | jq .root.parent.$srcKey | sed -e 's/\\\"/\"/g' -e 's/^.//g' -e 's/.$//g')
I am running the code inside a bash script on mac.
This approach works just fine if the $srcKey variable does NOT have any hyphens in it, but breaks with the following error if it has hyphens
jq: error: Part1OfHypenatedKey/0 is not defined at <top-level>
jq: error: Part2OfHypenatedKey/0 is not defined at <top-level>
Tried putting just the variable with hyphens inside quotes - didnt make a difference
value=$(echo $srcJson | jq .root.parent."$srcKey" | sed -e 's/\\\"/\"/g' -e 's/^.//g' -e 's/.$//g')
Tried putting the entire jq key reference in quote - but didnt work at all, may be i was doing something wrong with the subshell commands & jq (not a bash expert)
value=$(echo $srcJson | jq ".root.parent.$srcKey" | sed -e 's/\\\"/\"/g' -e 's/^.//g' -e 's/.$//g')