Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jq不使用参数替换json值_Json_Variables_Jq_String Literals_String Interpolation - Fatal编程技术网

jq不使用参数替换json值

jq不使用参数替换json值,json,variables,jq,string-literals,string-interpolation,Json,Variables,Jq,String Literals,String Interpolation,test.sh没有替换test.json参数值($input1和$input2)。result.json具有相同的参数值“$input1/solution/$input2.result” test.sh jq脚本中的shell变量应通过--arg name value插入或作为参数传递: jq --arg inp1 "$input1" --arg inp2 "$input2" \ 'map(if .ParameterKey == "Project" then . + {"Paramet

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

test.sh 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
输出:

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

在jq程序中,您引用了“$input1/solution/$input2.result”,因此它是一个JSON字符串文本,而您显然需要字符串插值;您还需要一方面区分shell变量($input1和$input2),另一方面区分相应的jq美元变量(可能有或可能没有相同的名称)

由于shell变量是字符串,因此可以使用
--arg
命令行选项将它们传入(例如,如果您选择以相同的方式命名变量,
--arg input1“$input1”

您可以阅读jq手册中有关字符串插值的内容(请参阅,但请注意不同版本jq顶部的链接)

还有其他方法可以实现所需的结果,但是使用具有相同命名变量的字符串插值,您可以编写:

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

请注意,上面的字符串本身并不是一个JSON字符串。只有在字符串插值之后才会变成这样。

不要将shell值插值到
jq
脚本中;一般来说,您不知道结果将是一个有效的
jq
脚本。@chepner,是的,我更喜欢通过
--arg name value
传递变量。如果我删除json开头/结尾的方括号,它将不起作用。{“ParameterKey”:“Project”,“ParameterValue”:[“$input1/solution/$input2.result”]}@spiderman,如果去掉方括号,这将完全是另一个输入。另一个输入-另一个问题,另一个案例,而不是当前的
[
  {
    "ParameterKey": "Project",
    "ParameterValue": "test1/solution/test2.result"
  }
]
"\($input1)/solution/\($input2).result"