如何创建一个JSON
对象来显示两个不同的JSON
文件中每个键的值的差异?
这将是一个很长的帖子,但主要是由于我使用的JSON
文件。请原谅我。
我有一个源文件-original. json
{
"billLogAnalyticsAsDavisDataUnits": false,
"billingProvider": "INTERNAL",
"blockUIDate": 1652572799000,
"chatEnabled": false,
"customMetricsLimit": 9223372036854776000,
"customMetricsOverageLimit": 9223372036854776000,
"davisDataUnitsAnnualLimit": -1,
"davisDataUnitsEnabled": false,
"davisDataUnitsMigrated": false,
"davisDataUnitsMonthlyLimit": -1,
"demUnitsAnnualQuota": 0,
"demUnitsQuota": 0,
"expirationCounterEnabled": true,
"expirationTime": 1652572799000,
"externalApiQuota": 2147483647,
"hostUnitsCapping": {
"fullstackHostLimit": 0,
"hasContainersHostLimit": 0,
"hostUnitsCappingEnabled": false,
"infrastructureOnlyHostLimit": 0
},
"hostUnitsQuota": 2147483647,
"ibmSystemZCICSIMSMonitoring": false,
"infrastructureSupportedTechnologies": {
"infrastructureOnlySupport": true,
"logAgent": true,
"maxInfrastructureOnlyAgents": 9223372036854776000,
"networkAgent": true,
"pluginAgent": true
},
"iotEntityQuota": 20,
"iotTsQuota": 10,
"isConsumption": false,
"isCreditExhausted": false,
"isRumEnabled": false,
"licenseType": "PAYING",
"logAnalyticsIngressQuota": 9223372036854776000,
"logAnalyticsIngressQuotaAnnually": 9223372036854776000,
"logAnalyticsStorageEnabled": false,
"logAnalyticsStorageQuota": 0,
"maxActionsPerMinute": 3500,
"maxAgents": 2147483647,
"maxHostUnitsQuota": -1,
"maxPaasAgents": 2147483647,
"maxWebChecks": 9223372036854776000,
"maxWebChecksAnnual": 9223372036854776000,
"overageCustomMetrics": true,
"overageEnabled": false,
"replayStorageDomQuotaInMb": 10000,
"replayStorageDomRetention": 86400000,
"retentionCode": 864000000,
"retentionRum": 864000000,
"retentionService": 1209600000,
"retentionWebcheck": 864000000,
"rumAdditionalUserPropertiesEnabled": true,
"rumAdditionalUserPropertiesLowerLimit": 20,
"sessionReplayEnabled": false,
"sessionStorageQuota": 2147483647,
"suspensionType": "NONE",
"symbolicationFileStorageQuota": 1024,
"syntheticEnabled": false,
"useHostUnitWeighting": false,
"visitsAnnualQuota": -1,
"visitsQuota": -1
}
我需要替换存储在template.json文件中的具有新值的一些键
{
"billLogAnalyticsAsDavisDataUnits": false,
"billingProvider": "INTERNAL",
"blockUIDate": 1652572799000,
"chatEnabled": false,
"demUnitsAnnualQuota": -1,
"demUnitsQuota": -1,
"isRumEnabled": false,
"licenseType": "PAYING",
"logAnalyticsIngressQuota": -1,
"logAnalyticsIngressQuotaAnnually": -1,
"logAnalyticsStorageEnabled": false,
"logAnalyticsStorageQuota": 0,
"overageCustomMetrics": true,
"overageEnabled": false,
"replayStorageDomQuotaInMb": 10000,
"replayStorageDomRetention": 86400000,
"retentionCode": 864000000,
"retentionRum": 864000000,
"retentionService": 1209600000,
"retentionWebcheck": 864000000,
"rumAdditionalUserPropertiesEnabled": true,
"rumAdditionalUserPropertiesLowerLimit": 20,
"sessionReplayEnabled": false,
"sessionStorageQuota": 102400,
"suspensionType": "NONE",
"symbolicationFileStorageQuota": 1024,
"syntheticEnabled": true,
"useHostUnitWeighting": false,
"visitsAnnualQuota": 0,
"visitsQuota": -1
}
我正在用jq的下面的电话
jq -n --argfile original.json --argfile template template.json '$original |$original +=$template' >updated.json
获取新的updated. json文件,并将其提交给API服务器。
API服务器处理后发出一个结构完全相同的新文件。但是,某些键值可能会更改。下面是从API服务器下载的文件-downloaded. json
{
"billLogAnalyticsAsDavisDataUnits": false,
"billingProvider": "INTERNAL",
"blockUIDate": 1652572799000,
"chatEnabled": false,
"customMetricsLimit": 9223372036854776000,
"customMetricsOverageLimit": 9223372036854776000,
"davisDataUnitsAnnualLimit": -1,
"davisDataUnitsEnabled": true,
"davisDataUnitsMigrated": false,
"davisDataUnitsMonthlyLimit": -1,
"demUnitsAnnualQuota": -1,
"demUnitsQuota": -1,
"expirationCounterEnabled": true,
"expirationTime": 0,
"externalApiQuota": 2147483647,
"hostUnitsCapping": {
"fullstackHostLimit": 0,
"hasContainersHostLimit": 0,
"hostUnitsCappingEnabled": false,
"infrastructureOnlyHostLimit": 0
},
"hostUnitsQuota": -1,
"ibmSystemZCICSIMSMonitoring": false,
"infrastructureSupportedTechnologies": {
"infrastructureOnlySupport": true,
"logAgent": true,
"maxInfrastructureOnlyAgents": 9223372036854776000,
"networkAgent": true,
"pluginAgent": true
},
"iotEntityQuota": 20,
"iotTsQuota": 10,
"isConsumption": false,
"isCreditExhausted": false,
"isRumEnabled": true,
"licenseType": "PAYING",
"logAnalyticsIngressQuota": -1,
"logAnalyticsIngressQuotaAnnually": -1,
"logAnalyticsStorageEnabled": false,
"logAnalyticsStorageQuota": 0,
"maxActionsPerMinute": 3500,
"maxAgents": 2147483647,
"maxHostUnitsQuota": -1,
"maxPaasAgents": 2147483647,
"maxWebChecks": 9223372036854776000,
"maxWebChecksAnnual": 9223372036854776000,
"overageCustomMetrics": true,
"overageEnabled": false,
"replayStorageDomQuotaInMb": 10000,
"replayStorageDomRetention": 86400000,
"retentionCode": 864000000,
"retentionRum": 864000000,
"retentionService": 1209600000,
"retentionWebcheck": 864000000,
"rumAdditionalUserPropertiesEnabled": true,
"rumAdditionalUserPropertiesLowerLimit": 20,
"sessionReplayEnabled": false,
"sessionStorageQuota": 102400,
"suspensionType": "NONE",
"symbolicationFileStorageQuota": 1024,
"syntheticEnabled": true,
"useHostUnitWeighting": false,
"visitsAnnualQuota": 0,
"visitsQuota": -1
}
下面是我的任务-我需要找到updated. json和downloaded. json之间的差异
我正在使用diff
diff <(jq -S . update.json) <(jq -S . downloaded.json)
它产生以下结果
< "davisDataUnitsEnabled": false,
---
> "davisDataUnitsEnabled": true,
15c15
< "expirationTime": 1652572799000,
---
> "expirationTime": 0,
23c23
< "hostUnitsQuota": 2147483647,
---
> "hostUnitsQuota": -1,
36c36
< "isRumEnabled": false,
---
> "isRumEnabled": true,
我想要的是-用原始文件(来自updated.json)和新文件(来自downloaded.json)创建一个新对象,所以它应该看起来像下面这样:
"Original": {
"davisDataUnitsEnabled": false,
"expirationTime": 1652572799000,
"hostUnitsQuota": 2147483647,
"isRumEnabled": false
},
"Updated": {
"davisDataUnitsEnabled": true,
"expirationTime": 0,
"hostUnitsQuota": -1,
"isRumEnabled": true
}
}
我试着用to_entries
和from_entries
看看更新和下载的区别,用下面的jq命令把所有结合在一起:
jq -n \
--argfile original original.json \
--argfile download downloaded.json \
--argfile template template.json \
'$original |$original +=$template |
($original | to_entries) as $x |
($download | to_entries) as $y |
$y - $x | from_entries'
但是,与diff相比,输出有很大不同:
{
"davisDataUnitsEnabled": true,
"demUnitsAnnualQuota": -1,
"demUnitsQuota": -1,
"expirationTime": 0,
"hostUnitsQuota": -1,
"isRumEnabled": true,
"logAnalyticsIngressQuota": -1,
"logAnalyticsIngressQuotaAnnually": -1,
"sessionStorageQuota": 102400,
"syntheticEnabled": true,
"visitsAnnualQuota": 0
}
上面的输出中还列出了七个键,而不是diff中的四个,这七个键在updated.json
和downloaded.json
中的值完全相同
我的问题是什么原因导致这七个额外的键出现在jq输出中?我可以用jq得到值不同的键的正确输出,并按照我想要的方式格式化输出吗?
- -P.S.经过一番挖掘,发现
comm
给了我想要的输出
comm --nocheck-order -13 <(jq -S . updated.json) <(jq -S . downloaded.json)
它产生输出:
"davisDataUnitsEnabled": true,
"expirationTime": 0,
"hostUnitsQuota": -1,
"isRumEnabled": true,
现在,如果我可以通过JQ
将输出放在"Updated"
对象下,同时忽略后面的逗号,这可能就是最终结果了。
- 更新**我发现了问题所在,并在下面写下了答案。让问题保持原样,因为有人可能会遇到同样的情况。
1条答案
按热度按时间rhfm7lfc1#
在发布问题并重新检查下面的
jq
命令之后,我发布了一个声明,我没有将原始文件和模板文件合并在一个变量中,所以原始的、未经修改的文件被分配给var$x
。相反,产生我想要的结果的正确
jq
命令如下:它产生正确的输出: