jq不使用参数替换json值

spi*_*man 4 variables json string-literals string-interpolation jq

test.sh不会替换test.json参数值($ input1和$ input2)。result.json具有相同的ParameterValue“ $ input1 / solution / $ input2.result”

 [
    {
      "ParameterKey": "Project",
      "ParameterValue": [ "$input1/solution/$input2.result" ]
     }
    ]
Run Code Online (Sandbox Code Playgroud)

test.sh

#!/bin/bash
input1="test1"
input2="test2"
echo $input1
echo $input2
cat test.json | jq 'map(if .ParameterKey == "Project"           then . + {"ParameterValue" : "$input1/solution/$input2.result" }             else .             end           )' > result.json
Run Code Online (Sandbox Code Playgroud)

Rom*_*est 6

jq脚本中的shell变量应通过以下命令插入或作为参数传递--arg name value

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

输出:

[
  {
    "ParameterKey": "Project",
    "ParameterValue": "test1/solution/test2.result"
  }
]
Run Code Online (Sandbox Code Playgroud)