This question already has answers here:
Passing variables (containing spaces) to curl --data field
(3 answers)
Closed 17 days ago.
When I try to add a bash string, that contains spaces with jq an error message will be generated. The error doesn't occur, if there are no spaces.
the code that generates the error
value="value with spaces"
echo {} | jq ". +={"key":"'$value'"}"
error message
jq: error: syntax error, unexpected $end, expecting QQSTRING_TEXT or QQSTRING_INTERP_START or QQSTRING_END (Unix shell quoting issues?) at <top-level>, line 1: . +={"key":"value jq: 1 compile error
expected output
{"key":"value with spaces"}
note
without spaces in the value bash variable, the command will run just fine
You need to properly format the string you are using in jq.
You can create the variable in the same way:
VAL="value with spaces"
And using it like this:
jq -n --arg myvar "${VAL}" '{key: $myvar}'
{
"key": "value with spaces"
}
Related
This question already has answers here:
How to use jq when the variable has reserved characters?
(3 answers)
Closed last month.
Assume the following output from a REST API (stored as test.json here):
{
"system": {
"power": 10.5,
"%mem": 0.5,
"%cpu": 12.4
}
}
I can easily query the first item:
$ jq '.system.power' test.json
10.5
However:
$ jq '.system.%cpu' test.json
jq: error: syntax error, unexpected '%', expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
.system.%cpu
jq: 1 compile error
According to JSON.org, a string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. Any valid string can be used as a JSON key. So %cpu seems indeed valid and, indeed, jq can handle this elsewhere just fine:
$ jq '.system | has("%cpu")' test.json
true
Question: How can I extract the lower two properties from the system object?
JSON keys are always strings, but jq sometimes allows to use them without string (if they would be a valid "identifier"). You must quote it if it contains special characters such as %:
jq '.system."%cpu"' test.json
or jq '.system["%cpu"]' test.json
This question already has answers here:
Escape field name in jq that contains '#' and '-'? [duplicate]
(2 answers)
Closed 3 years ago.
It seems unlikely this hasn't been asked, but I can not find it anywhere.
I am trying to filter response from elasticsearch and need to reference the #timestamp field. However, jq uses # to specify formatters and I find nothing in documentation about how to escape the # character.
$ jq '.hits.hits[] | ._source.#timestamp' < debug.response.json
jq: error: syntax error, unexpected $end, expecting QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
.hits.hits[] | ._source.#timestamp
jq: 1 compile error
You can use the generic object index syntax, like:
.hits.hits[]._source["#timestamp"]
This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
When to wrap quotes around a shell variable?
(5 answers)
Closed 5 years ago.
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 :)
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:.}]}'
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.
I'm trying to make a GET request with curl in a bash file.
So I call curl, and get a JSON response from my server, and I try to get the object line which is part of the response, and I do so with jq, so I can print the value.
But its not working.
My response is something like this:
{"line":"Azul", "id":"j3453j45n35", "lat":"39.091937", "long":"-9.265441"}
My bash script:
#!/bin/bash
while :
do
cmd=$(curl -i -H "Content-Type: application/json" http://localhost:8080/cards/exists/1 | jq ‘.”line”’)
sleep 2
done
The result:
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Unix shell quoting issues?) at , line 1:
‘.”line”’
jq: 1 compile error
(23) Failed writing body
Whats going wrong?
I followed plenty of responses people advised online, but I keep getting the same response.
My json object seems to be correct.
I'm running this file on MacOS Sierra
There are two problems with your code:
You're using non-ASCII quotes (‘ ” ” ’), which jq doesn't understand:
Replace them with their ASCII equivalents: ' " " '
On macOS, avoid a text editor such as the standard TextEdit.app that has the "smart quotes" feature turned on, or turn that feature off via the menu system: Edit > Substitutions > Smart Quotes
It is this feature that replaces ASCII quoting chars. with their non-ASCII, typographical equivalents, as you type.
Generally, consider using a dedicated source-code editor for editing your code, such as Visual Studio Code (free) or Atom (free) or Sublime Text (paid).
Your curl command includes header output (-i), which jq chokes on - simply omit the -i:
#!/bin/bash
while :; do
cmd=$(curl -H 'Content-Type: application/json' 'http://localhost:8080/cards/exists/1' |
jq '.line')
sleep 2
done
While '."line"' would have worked too, there's no need for the extra layer of quoting for simple property names like that.
Also, it's better to enclose strings that you want to be interpreted literally in '...' in the shell.
How to use an angle brackets in command line arguments?
Suppose the input is: filename -w <input.txt>
Here input.txt is a second command line argument, and it is an input file to a program here input.txt must be enclosed by angle brackets( <input.txt> ) . But if I use angular brackets i m getting error. error is :
sh: -c: line 0: syntax error near unexpected token `newline'
sh: -c: line 0: `demo -w '
Your question is not very clear. However, it seems to me a shell problem, not a Tcl one.
I suppose your filename is the name of a Tcl executable script and you want to give it input.txt as parameter.
But the angle brackets have special meaning to the shell. sh interprets the < as standard input redirecting, so it reads the content of input.txt and passes it to filename through the standard input channel.
Then, sh interprets the > as standard output redirecting, but after that there is no name to redirect the output to, so it gives you the error you see, because after > you pressed the Return key.
So, are you sure your script needs the filename surrounded by angle brackets? If so, escape the brackets, like
filename -w \<input.txt\>
Otherwise, try to completely remove the brackets and use
filename -w input.txt
I hope this helps.