Home > Net >  jq concatenation with special characters
jq concatenation with special characters

Time:06-03

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:

CodePudding user response:

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.

CodePudding user response:

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