基于键值对从JSON文件中删除整个对象



我一直在努力从Json文件中删除偏移对象。我尝试了使用jqjson语法分析器方法,但没有成功。有人能帮忙吗。

我要寻找的是——无论下面的键和值对在文件中出现在哪里,都应该删除整个对象。

{"name": "exception"}

输入:

{
"results": [
{
"id": "a21f5193-881e-11eb-a0c1-3726f4a71fa9",
"retailerId": "1",
"category": "exception",
"context": {
"sourceEvents": [
"902bd449-881e-11eb-b603-29eb6c297e7d"
],
"entityType": "ORDER"
},
"eventStatus": "FAILED",
"attributes": [
{
"name": "exception",
"value": {
"code": 400,
"message": "Failed to execute http call",
"stackTrace": [
{
"fileName": "ReadOnlyFluentApiClient.java",
"className": "com.fluentretail.api.v2.client.ReadOnlyFluentApiClient"
}
],
"suppressed": [],
"suppressedExceptions": []
},
"type": "OBJECT"
},
{
"name": "lastRule",
"value": "ETOSUAT.base.ProposedFulfilmentWithoutInventory",
"type": "String"
},
{
"name": "lastRuleSet",
"value": "FindAndCreateDigitalFulfilment",
"type": "String"
},
{
"name": "message",
"value": "Failed to execute http call",
"type": "String"
}
],
"source": null,
"generatedBy": "Rubix User",
"generatedOn": "2021-03-18T19:17:51.517+0000"
},
{
"id": "a21f5193-881e-11eb-a0c1-3726f4a71fa9",
"retailerId": "1",
"category": "exception",
"context": {
"sourceEvents": [
"902bd449-881e-11eb-b603-29eb6c297e7d"
],
"entityType": "ORDER"
},
"eventStatus": "FAILED",
"attributes": [
{
"name": "exception",
"value": {
"code": 400,
"message": "Failed to execute http call",
"stackTrace": [
{
"fileName": "ReadOnlyFluentApiClient.java",
"className": "com.fluentretail.api.v2.client.ReadOnlyFluentApiClient"
}
],
"suppressed": [],
"suppressedExceptions": []
},
"type": "OBJECT"
},
{
"name": "lastRule",
"value": "ETOSUAT.base.ProposedFulfilmentWithoutInventory",
"type": "String"
},
{
"name": "lastRuleSet",
"value": "FindAndCreateDigitalFulfilment",
"type": "String"
},
{
"name": "message",
"value": "Failed to execute http call",
"type": "String"
}
],
"source": null,
"generatedBy": "Rubix User",
"generatedOn": "2021-03-18T19:17:51.517+0000"
}
]
}

预期输出为-

{
"results": [
{
"id": "a21f5193-881e-11eb-a0c1-3726f4a71fa9",
"retailerId": "1",
"category": "exception",
"context": {
"sourceEvents": [
"902bd449-881e-11eb-b603-29eb6c297e7d"
],
"entityType": "ORDER"
},
"eventStatus": "FAILED",
"attributes": [
{
"name": "lastRule",
"value": "ETOSUAT.base.ProposedFulfilmentWithoutInventory",
"type": "String"
},
{
"name": "lastRuleSet",
"value": "FindAndCreateDigitalFulfilment",
"type": "String"
},
{
"name": "message",
"value": "Failed to execute http call",
"type": "String"
}
],
"source": null,
"generatedBy": "Rubix User",
"generatedOn": "2021-03-18T19:17:51.517+0000"
},
{
"id": "a21f5193-881e-11eb-a0c1-3726f4a71fa9",
"retailerId": "1",
"category": "exception",
"context": {
"sourceEvents": [
"902bd449-881e-11eb-b603-29eb6c297e7d"
],
"entityType": "ORDER"
},
"eventStatus": "FAILED",
"attributes": [
{
"name": "lastRule",
"value": "ETOSUAT.base.ProposedFulfilmentWithoutInventory",
"type": "String"
},
{
"name": "lastRuleSet",
"value": "FindAndCreateDigitalFulfilment",
"type": "String"
},
{
"name": "message",
"value": "Failed to execute http call",
"type": "String"
}
],
"source": null,
"generatedBy": "Rubix User",
"generatedOn": "2021-03-18T19:17:51.517+0000"
}
]
}
del(..|select(type=="object" and .name=="exception"))

试试看https://jqplay.org/s/il12Ribpdb

walk(if type=="object" and .name == "exception"
then empty else . end)

等效:

walk(select(type=="object" and .name == "exception" | not))

相关内容

  • 没有找到相关文章

最新更新