I am trying to append some Grafana dashboard query things to existing queries. The Select works and doing a simple =+ "TEST" added successfully.
The actual append I have has {} * and ""
") * on(instance, process_id) group_left(name, display_name, run_as) windows_service_info\{display_name=~"$variable",job="$job"\})
so the jq is
jq '. | (.dashboard.panels[].targets[].expr | select(contains("sum((rate(wmi"))) += ") * on(instance, process_id) group_left(name, display_name, run_as) windows_service_info\{display_name=~"$variable",job="$job"\})"'
I tried the string literal
#text {"text":") * on(instance, process_id) group_left(name, display_name, run_as) windows_service_info\{display_name=~"$variable",job="$job"\})"}'
getting errors like:
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
As json.org says, "any codepoint except " or \ or control characters" can be included within a JSON string without being escaped.
In the question, the given string does contain both " and \, so to convert that string to a valid JSON string, both would need to be escaped.
You need to escape quotation marks inside a quoted string. And the initial .| is redundant:
jq '(.dashboard.panels[].targets[].expr | select(contains("sum((rate(wmi"))) += ") * on(instance, process_id) group_left(name, display_name, run_as) windows_service_info{display_name=~\"$variable\",job=\"$job\"})"'
\{ and \} must not be escaped. Unless you require a literal \{ in your output, then your JQ string must contain \\{ and \\}.
Related
I can't use environment variables inside a string in jmespath. Using a wildcard could have also worked but it didn't work in my case either. Any leads on this issue would be appreciated thanks!
aws logs describe-log-groups | jq -r --arg CLUSTER_NAME "$EKS_CLUSTER_NAME" '.logGroups[] | select(.logGroupName == '/aws/eks/$CLUSTER_NAME/cluster')'
Output:
jq: error: syntax error, unexpected '/' (Unix shell quoting issues?) at <top-level>, line 1:
.logGroups[] | select(.logGroupName == /aws/eks//cluster)
jq: 1 compile error
You have two issues:
Your inner jq quotes conflict with the outer shell quotes. Replace the inner ones with double quotes ", which is the way to use string literals in jq.
To include a variable's value inside a string in jq you would either add up the parts (e.g. using + as in ".." + $var + ".."), or use string interpolation "..\($var).."
Try
jq -r --arg CLUSTER_NAME "$EKS_CLUSTER_NAME" '
.logGroups[] | select(.logGroupName == "/aws/eks/\($CLUSTER_NAME)/cluster")
'
I have been trying to reduce the array to a string to be used in string interpolation.
For example.
input = ["123", "456"]
expected output = array=123,456
Here is my try
$ echo '["123", "456"]' | jq 'array=\(.|join(","))'
jq: error: syntax error, unexpected INVALID_CHARACTER (Unix shell quoting issues?) at <top-level>, line 1:
array=\(.|join(","))
jq: 1 compile error
Using string interpolation \(.), you can do something like below. Your idea is almost right, but interpolation using \(..) needs the filter to present be inside a string with the expression to be used defined inside parens after a backslash
jq --raw-output '"array=\(join(","))"'
echo '["123", "456"]' | jq -r '"array=" + join(",")'
I have a JSON string without the separator ,. How do I parse it using jq?
$echo '{"access_token":"XXXX""expires_in":300"token_type":"Bearer"}' | jq -r .access_token
The above line gives me the below error:
parse error: Expected separator between values at line 1
I understand that the issue is because the JSON string provided is not comma-separated. But this is what I am getting as a response from the server. How do I parse such a string? I want to retrive the value for key "access_token".
You can use a regular expression with sed if you know the accesss token never contains quotes.
echo '{"access_token":"XXXX""expires_in":300"token_type":"Bearer"}' |
sed 's/"access_token":"\([^"]*\)/\1/'
The capture group between \( and \) captures the string between the quotes, and \1 in the replacement string extracts it.
Here are two just-jq solutions, each with its own degree of brittleness. The first one attempts to convert each entire input line into valid JSON:
Using fromjson
echo '{"access_token":"XXXX""expires_in":300"token_type":"Bearer"}' |
jq -rR 'gsub("(?<k>\"[^\"]*\")"; "," + .k )
| gsub("{,\"";"{\"") | gsub(":,\""; ":\"")
| fromjson | .access_token'
XXXX
Assume the value is a string on the same line
jq -rR 'sub(".*\"access_token\" *: *\"(?<v>[^\"]*)\".*"; .v )'
I can't really figure this out - the SQL query outputed is not valid
key="test"
payload=$(gzip -ckqd ./temp.json.gz | jq -c . | sed 's/"/\\"/g')
printf 'INSERT INTO my_table VALUES ("%s", "%s")' "$key" "$payload" | sqlite3 ./temp.db
Obiously the $payload variable is a json string (can have single and double quotes etc)
In SQL, strings are delimited not with double quotes but with single quotes.
Inside a string, the only special character is the single quote itself, and it must be escaped not with a backslash, but with another quote:
... sed "s/'/''/g"
I am trying to parse json files that contain sequences of slashes and backslashes in some of their strings like this:
echo '{"tag_string":"/\/\/\ test"}' | jq
which gives me:
parse error: Invalid escape at line 1, column 27
I have tried escaping with backslashes at different positions, but I can't seem to find a correct way. How do I output the string as it is, without removing any character or getting errors?
This only works on bash, but not sh (or zsh):
echo '{"tag_string":"/\\/\\/\\ test"}' | jq -r '.tag_string'
/\/\/\ test
A forward slash character is legal, but a single backslash character is not. According to json.org char description, the valid chars are:
char
any-Unicode-character-
except-"-or-\-or-
control-character
\"
\\
\/
\b
\f
\n
\r
\t
\u four-hex-digits
So in your example, the single backslashes are not legal, you need either "\\" which is interpreted as double backslashes, or you need to remove them entirely.
If you are trying to include literal backslashes:
(bash)
echo '{"tag_string":"/\\/\\/\\ test"}' | jq
{
"tag_string": "/\\/\\/\\ test"
}
echo '{"tag_string":"/\\/\\/\\ test"}' | jq -r '.["tag_string"]'
/\/\/\ test
(sh)
echo '{"tag_string":"/\\\\/\\\\/\\\\ test"}' | jq -r '.["tag_string"]'
/\/\/\ test
printf "%s" '{"tag_string":"/\\/\\/\\ test"}' | jq -r '.["tag_string"]'
/\/\/\ test
If you are trying to convert a file with non-JSON strings, then consider a tool such as any-json. Using the "cson-to-json" mode, "\/" will be interpreted as "/":
$ any-json -format=cson
Input:
{"tag_string":"/\/\/\ test"}
Output:
{
"tag_string": "/// test"
}