Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

jq argjson does not work with string from variable

I am trying to create a json with jq

f=1
jq -n --argjson a ${f} '{"test":$a}'

or

f="1"
jq -n --argjson a ${f} '{"test":$a}'

or

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

jq -n --argjson a "teststring" '{"test":$a}'

work fine

but

f="teststring"
jq -n --argjson a ${f} '{"test":$a}'

yields
jq: invalid JSON text passed to –argjson

What am I doing wrong?

Cheers
Gordon

>Solution :

Your shell is interpreting the double quotes and passing bare strings to jq.

The correct way to define your input is to add an extra level of quoting to make the double quotes part of the value:

f='"teststring"'
jq -n --argjson a ${f} '{"test":$a}'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading