Monday, April 30, 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

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...