jq not replacing json value with parameter jq not replacing json value with parameter json json

jq not replacing json value with parameter


shell variables in jq scripts should be interpolated or passed as arguments via --arg name value:

jq --arg inp1 "$input1" --arg inp2 "$input2" \'map(if .ParameterKey == "Project"     then . + {"ParameterValue" : ($inp1 + "/solution/" + $inp2 + ".result") } else . end)' test.json

The output:

[  {    "ParameterKey": "Project",    "ParameterValue": "test1/solution/test2.result"  }]


In your jq program, you have quoted "$input1/solution/$input2.result", and therefore it is a JSON string literal, whereas you evidently want string interpolation; you also need to distinguish between the shell variables ($input1 and $input2) on the one hand, and the corresponding jq dollar-variables (which may or may not have the same name) on the other.

Since your shell variables are strings, you could pass them in using the --arg command-line option (e.g. --arg input1 "$input1" if you chose to name the variables in the same way).

You can read up on string interpolation in the jq manual (see https://stedolan.github.io/jq/manual, but note the links at the top for different versions of jq).

There are other ways to achieve the desired results too, but using string interpolation with same-named variables, you'd write:

"\($input1)/solution/\($input2).result" 

Notice that the above string is NOT itself literally a JSON string. Only after string interpolation does it become so.