jq-从两个JSON文件的键值中的diff创建一个新对象

jq-从两个JSON文件的键值中的diff创建一个新对象,json,comparison,diff,jq,Json,Comparison,Diff,Jq,如果两个不同的JSON文件中存在这样的键,那么如何创建一个JSON对象来显示每个键的值的差异 这将是一篇很长的文章,但主要是由于我使用的JSON文件。请容忍我 我有一个源文件-original.json { "billLogAnalyticsAsDavisDataUnits": false, "billingProvider": "INTERNAL", "blockUIDate": 16525727990

如果两个不同的
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
}
{
  "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
}

我需要替换一些具有存储在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服务器下载的文件-download.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
}
{
  "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
}

这是我的任务-我需要找出更新的.json下载的.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
}
{
  "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
}

我正在使用diff

diff <(jq -S . update.json) <(jq -S . downloaded.json)
我试着使用
到_条目
从_条目
查看更新和下载之间的差异,并将以下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获得值不同的键的正确输出并按我想要的方式格式化输出吗

-- 另外,经过一番挖掘,我发现
comm
正在给我想要的输出

comm --nocheck-order -13 <(jq -S . updated.json) <(jq -S . downloaded.json)
现在,如果我可以通过
JQ
将该输出放在
“Updated”
对象下,同时忽略后面的逗号,那么这可能就是最终结果


更新我找出了问题所在,并将答案写在下面。保留问题的原样,因为有人可能会遇到相同的情况。

只有在发布问题并重新检查以下
jq
命令后,我才发布我没有将合并的原始文件和模板文件保存在变量中,因此原始的未修改文件被分配给var
$x

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
相反,生成我想要的结果的正确的
jq
命令如下:

`jq` -n \
--argfile original original.json \
--argfile download downloaded.json \
--argfile template template.json \
'$original |($original +=$template | to_entries) as $x |
($download | to_entries) as $y | 
($y - $x | from_entries) as $new |
($x - $y | from_entries) as $old |
| [{"Original":$old,"Updated": $new}]'
这将产生正确的输出:

[
  {
    "Original": {
      "davisDataUnitsEnabled": false,
      "expirationTime": 1652572799000,
      "hostUnitsQuota": 2147483647,
      "isRumEnabled": false
    },
    "Updated": {
      "davisDataUnitsEnabled": true,
      "expirationTime": 0,
      "hostUnitsQuota": -1,
      "isRumEnabled": true
    }
  }
]