使用jq动态修改bash脚本中的JSON文件-使用heredocs或密钥分配?

使用jq动态修改bash脚本中的JSON文件-使用heredocs或密钥分配?,json,bash,jq,Json,Bash,Jq,下面是我的JSON文件的一部分,JSON文件本身更长 { "anomalyDetection": { "loadingTimeThresholds": { "enabled": false, "thresholds": [] }, "outageHandling": {

下面是我的JSON文件的一部分,JSON文件本身更长

{
    "anomalyDetection": {
        "loadingTimeThresholds": {
            "enabled": false,
            "thresholds": []
        },
        "outageHandling": {
            "globalOutage": true,
            "localOutage": true,
            "localOutagePolicy": {
                "affectedLocations": 1,
                "consecutiveRuns": 2
            }
        }
    }
}
在这里,我需要使用提供的参数修改
affectedLocations
continuentiveruns
值,将修改后的JSON分配给一个变量,然后在API调用期间使用该变量

我用bash创建了两个不同的解决方案来完成上述任务

选项1-使用heredocs

# Assign the template file to a template var
get_template() {
    read -r -d '' template <<JSONTEMPLATE
{
    "anomalyDetection": {
        "loadingTimeThresholds": {
            "enabled": false,
            "thresholds": []
        },
        "outageHandling": {
            "globalOutage": true,
            "localOutage": true,
            "localOutagePolicy": {
                "affectedLocations": 1,
                "consecutiveRuns": 2
            }
        }
    }
}
JSONTEMPLATE
}

modify_json_with_heredoc() {

    # Call the template var assignment
    get_template

    read -r -d '' json_keys_updated <<JSONSTRING
{
    "globalOutage": true,
    "localOutage": true,
    "localOutagePolicy": {
        "affectedLocations": ${1:-1},
        "consecutiveRuns": ${2:-2}
    }
}
JSONSTRING

    # Replace key values from the template with the updated parameters

    updated_JSON=$(jq --argjson update_outageHandling "$json_keys_updated" \
        '.anomalyDetection.outageHandling|=$update_outageHandling' \
        <<<"$template")
}
#将模板文件分配给模板变量
获取模板(){

read-r-d“”模板另一种选择是处理
jq
中的所有参数:

!/usr/bin/env sh
#将模板文件分配给模板变量
获取模板(){
模板{
“异常检测”:{
“loadingTimeThresholds”:{
“启用”:false,
“阈值”:[]
},
“outageHandling”:{
“环球旅行”:没错,
“本地中断”:正确,
“localOutagePolicy”:{
“受影响地点”:1,
“连续性”:2
}
}
}
}'
}
修改_json(){
#调用模板分配
获取模板
更新的JSON=$(
jq\
--参数更新\u影响位置“$1”\
--参数更新\u连续运行“$2”\
--空输入\
“$template”|
如果$update_影响位置|请尝试编号捕捉false
然后
.anomalyDetection.outageHandling.localOutagePolicy.affectedLocations |=(
$update_受影响的位置|编号
)
其他的
结束|
如果$update_连续运行|请尝试使用数字捕捉false
然后
.anomalyDetection.outageHandling.localOutagePolicy.ConcertiveRuns |=(
$update_CONCEDUTIVERUNS|tonumber
)
其他的
结束'
)
}
修改json 42 666
echo“$updated_JSON”

出于以下原因,我也建议采用不同的方法

#!/usr/bin/env bash

function template {
    cat<<EOF
{
    "anomalyDetection": {
        "loadingTimeThresholds": {
            "enabled": false,
            "thresholds": []
        },
        "outageHandling": {
            "globalOutage": true,
            "localOutage": true,
            "localOutagePolicy": {
                "affectedLocations": 1,
                "consecutiveRuns": 2
            }
        }
    }
}
EOF
}

function modify_json() {
    template |  
    jq --argjson update_affectedLocations "${1:-1}" \
       --argjson update_consecutiveRuns "${2:-2}" '
       .anomalyDetection.outageHandling.localOutagePolicy
         |= (.affectedLocations |= $update_affectedLocations
             | .consecutiveRuns |= $update_consecutiveRuns )
    '
}

updated_JSON=$(modify_json 42 666)
echo "$updated_JSON"
!/usr/bin/env bash
功能模板{

Cat您想要的答案是什么类型的?似乎您已经提出了一些建议。我想说,鉴于jq是为json修订而构建的,这将是更好的解决方案。您还可以使用time命令测试这两个选项的效率。@Inian-虽然我有两个工作选项,但问题是-来自performance/usability/code safety透视图哪一个更可取。使用
--arg
而不是
--arg json
,因为您的参数不是json而是普通的shell字符串。@LéaGris在使用--arg jq时将参数解释为字符串。因此,您将得到“3”而不是键分配中的3。使用--argjson,您不会有这个问题。使用--arg,然后如果可以让--argjson获得相同的结果,则执行其他逻辑有什么意义?还有,您的代码如何处理
影响位置
连续运行
的默认值?它们在模板中的事实失败并不意味着不需要它们-模板文件中的值可能已丢失/修改。谢谢。我采纳了您的建议,然后对其进行了轻微修改-而不是
template | jq'.'.'.
我使用了流程替换
jq'.''''<
#!/usr/bin/env bash

function template {
    cat<<EOF
{
    "anomalyDetection": {
        "loadingTimeThresholds": {
            "enabled": false,
            "thresholds": []
        },
        "outageHandling": {
            "globalOutage": true,
            "localOutage": true,
            "localOutagePolicy": {
                "affectedLocations": 1,
                "consecutiveRuns": 2
            }
        }
    }
}
EOF
}

function modify_json() {
    template |  
    jq --argjson update_affectedLocations "${1:-1}" \
       --argjson update_consecutiveRuns "${2:-2}" '
       .anomalyDetection.outageHandling.localOutagePolicy
         |= (.affectedLocations |= $update_affectedLocations
             | .consecutiveRuns |= $update_consecutiveRuns )
    '
}

updated_JSON=$(modify_json 42 666)
echo "$updated_JSON"