Search code examples
jsonvariablesjqstring-literalsstring-interpolation

jq not replacing json value with parameter


test.sh is not replacing test.json parameter values ($input1 and $input2). result.json has same ParameterValue "$input1/solution/$input2.result"

 [
    {
      "ParameterKey": "Project",
      "ParameterValue": [ "$input1/solution/$input2.result" ]
     }
    ]

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

Solution

  • 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"
      }
    ]