0

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 <top-level>, line 1:
world"}     
jq: 1 compile error

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

Thanks for any help :)

peak
  • 105,803
  • 17
  • 152
  • 177
tarteflambee
  • 13
  • 1
  • 5

2 Answers2

2

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:.}]}'
oliv
  • 12,690
  • 25
  • 45
0

You've encountered a shell issue: you are missing quotation marks around $(<$1). The hint is that the space makes a difference.

By the way, when there are several moving parts (as there are here), it would be wise to try to isolate the problem before resorting to stackoverflow. That way, you will often solve the problem yourself; if not, it will at least make it easier for others to focus on the real (and hopefully interesting) issue.

peak
  • 105,803
  • 17
  • 152
  • 177
  • thank you, I’m new to shell and I was having a hard time identifying where the issue could be coming from… this whole quotation thing confuses me. :) – tarteflambee Dec 12 '17 at 15:15