Score:0

jq: error: X/0 is not defined at , line 1:

sa flag

I'm trying to extract value from the bellow JSON:

% export test='{"a-b-c":"x-y-z"}'
% echo $test
{"a-b-c":"x-y-z"}
% echo $test | jq .a-b-c
jq: error: b/0 is not defined at <top-level>, line 1:
.a-b-c
jq: error: c/0 is not defined at <top-level>, line 1:
.a-b-c
jq: 2 compile errors
% echo $test | jq '."a-b-c"'
"x-y-z"
%

while the last line "works", my end goal is to have shell script and substitute "a-b-c" parameter with variable, however due to I must use single quotes, the actual value isn't being passed..

% export var1=a-b-c
% echo $var1
a-b-c
% echo $test | jq '."$var1"'
null
%

Please advise)

Score:2
fo flag

Use the --arg option to initialize a jq variable with a shell variable's value.

jq --arg key "$var1" '.[$key]'

See Invoking jq and scroll down to --arg

Score:0
sa flag

I've got it to work:

% echo $test | jq ".\"$var1\""
"x-y-z"
%

Thanks!

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.