Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 340
spiderman Avatar asked Nov 19 '17 07:11

spiderman


1 Answers

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"
  }
]
like image 130
RomanPerekhrest Avatar answered Sep 28 '22 00:09

RomanPerekhrest