简体   繁体   English

jq - 当键/值在不同文件中时组合多个 if 条件

[英]jq - combining multiple if conditions when keys/values are in different files

I have two keys in the license.json JSON file - "visitsAnnualQuota" and "visitsQuota" at the very bottom:我在license.json JSON 文件中有两个键 - "visitsAnnualQuota""visitsQuota"

{
    "customMetricsLimit": 9223372036854776000,
    "customMetricsOverageLimit": 9223372036854776000,
    "UnitsAnnualQuota": 9223372036854776000,
    "UnitsQuota": 9223372036854776000,
    "expirationTime": 1652572799000,
    "infrastructureSupportedTechnologies": {
        "maxInfrastructureOnlyAgents": 9223372036854776000,
        "networkAgent": true,
        "pluginAgent": true
    },
    "logAnalyticsIngressQuota": 9223372036854776000,
    "logAnalyticsIngressQuotaAnnually": 9223372036854776000,
    "maxAgents": 2147483647,
    "maxHostUnitsQuota": -1,
    "maxPaasAgents": 2147483647,
    "maxWebChecks": 9223372036854776000,
    "maxWebChecksAnnual": 9223372036854776000,
    "replayStorageDomQuotaInMb": 9223372036854776000,
    "replayStorageDomRetention": 86400000,
    "retentionCode": 864000000,
    "retentionService": 1209600000,
    "retentionWebcheck": 864000000,
    "symbolicationFileStorageQuota": 1024,
    "syntheticEnabled": true,
    "useHostUnitWeighting": false,
    "visitsAnnualQuota": 120000,
    "visitsQuota": 10000
}

I need to set them to value 0 if the value of one particular key - .numberOfVisits from a different JSON file - consumption.json equals to zero.如果一个特定键的值 - 来自不同JSON文件的.numberOfVisits - consumption.json .numberOfVisits ,我需要将它们设置为值 0。 Here is the fragment of the consumption.json file这是consumption.json文件的片段

{
    "Header": {
        "Tenant": "Tenant-1"
    },
    "Body": [
        {
            "Metrics": [
                {
                    "timeframe": "Week",
                    "numberOfVisits": 0
                },
                {
                    "timeframe": "Month",
                    "numberOfVisits": 0
                },
                {
                    "timeframe": "Year",
                    "numberOfVisits": 247648006
                }
            ],
            "tags": [
                {
                    "context": "ENVIRONMENT",
                    "key": "srch"
                }
            ],
            "webServerName": "localhost"
        }
    ]
}

If the .numberOfVisits from consumption.json does not equal to zero, when timeframe="Week" or when timeframe="Month" in the same block, then the above two keys need to set to -1 and all other keys in the license.json file which have the value equal to 9223372036854776000 need to have that value replaced to -1.如果.numberOfVisitsconsumption.json等于零,当timeframe="Week" ,或者当timeframe="Month"在同一块中,则上述两个键需要设置为-1,并在所有其它密钥license.json值等于 9223372036854776000 的license.json文件需要将该值替换为 -1。

What I am doing right now is to have a bash script, read the value of .numberOfVisits to a shell variable, and then have a case block for the variable equals or not to zero and for each condition have jq called with the appropriate filters.我现在正在做的是有一个 bash 脚本,将.numberOfVisits的值读取到一个 shell 变量,然后有一个 case 块用于变量等于或不为零,并且对于每个条件都使用适当的过滤器调用jq

My question - can all the above logic combined into a single jq call?我的问题 - 可以将上述所有逻辑组合成一个jq调用吗?

Yes this can be done with jq in a single shot.是的,这可以用jq一次性完成。 All you need to do is use the --argfile option on the consumption.json and apply the condition on the numberOfVisits field所有你需要做的就是使用--argfile的选项consumption.json和应用的条件numberOfVisits

jq --argfile visits consumption.json '
    ( $visits | .Body | map( .Metrics[] | select(.numberOfVisits == 0)) | length ) as $data |
    if $data != 2 then
        with_entries( select(.value == 9223372036854776000).value = -1 ) |
          ( .visitsAnnualQuota, .visitsQuota ) |= -1
    elif $data == 2 then
        ( .visitsAnnualQuota, .visitsQuota ) |= 0
    else
        .
    end' license.json

The above approach calculates the number of occurrences of .numberOfVisits with value 0 and stores in $data variable.上述方法计算值为 0 的.numberOfVisits出现的.numberOfVisits并存储在$data变量中。 Then depending on the value in the variable, we apply the filter expressions to modify the fields as required.然后根据变量中的值,我们应用过滤器表达式来根据需要修改字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM