Monday, 30 April 2018

bash - jq not parsing json with spaces





I’m trying to run the following command that reads JSON from a file and formats it with jq :



jq -n -r --arg m $(<$1) '$m | fromjson | {records:[{value:.}]}'



It produces the desired output when the input JSON does not contain spaces, such as {"test":"helloworld"} :



{
"records": [
{
"value": {
"test": "helloworld"
}
}

]
}


However, for an input like {"test":"hello world"} it would give the following error:



jq: error: syntax error, unexpected QQSTRING_START, expecting $end (Unix shell quoting issues?) at , line 1:
world"}
jq: 1 compile error



Can’t figure out what’s causing this problem.



Thanks for any help :)


Answer



It's not a jq problem but a quoting issue (as highlighted in the error).



Change the --arg option to have the value within double quote:



arg='{"test":"hello world"}'

jq -n -r --arg m "$arg" '$m | fromjson | {records:[{value:.}]}'

No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...