jq不使用参数替换json值 [英] jq not replacing json value with parameter

查看:74
本文介绍了jq不使用参数替换json值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

jq 脚本中的

推荐答案

shell变量应通过--arg name value插入或作为参数传递:

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

输出:

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

这篇关于jq不使用参数替换json值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆