[英]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.如果.numberOfVisits
从consumption.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.