[英]Flatten array and reformat the JSON using jq
I'm trying to accomplish two things:我正在尝试完成两件事:
Here's is the json I'm working with:这是我正在使用的 json:
{
"id": 1,
"feed": "jqQuestion",
"organization": "question.com",
"category": "question",
"contact": {
"name": "",
"email": "",
"phone": ""
},
"website": "https://thankyou.com/",
"schedule": "daily",
"stamp_added": "2017-09-27 00:00:00",
"stamp_updated": "2022-09-10 15:51:09",
"stamp_pulled": "2022-09-10 08:51:03",
"stamp_modified": "2019-08-07 18:29:00",
"nextarray": {
"value": 1,
"value2": 2
}
}
I'm hoping to remove the "title" of the arrays, just keep the content, and align the contents of the array with the reset of the json:我希望删除arrays的“标题”,只保留内容,并将数组的内容与json的重置对齐:
{
"id": 1,
"feed": "jqQuestion",
"organization": "question.com",
"category": "question",
"name": "",
"email": "",
"phone": ""
"website": "https://thankyou.com/",
"schedule": "daily",
"stamp_added": "2017-09-27 00:00:00",
"stamp_updated": "2022-09-10 15:51:09",
"stamp_pulled": "2022-09-10 08:51:03",
"stamp_modified": "2019-08-07 18:29:00",
"value": 1,
"value2": 2
}
I've tried a few approaches, using sed, awk, and jq.我尝试了几种方法,使用 sed、awk 和 jq。 I think using jq would be "optimal", but haven't been able to get the format right with the mentioned approaches.
我认为使用 jq 将是“最佳的”,但无法通过上述方法获得正确的格式。 Any guidance here would be much appreciated!
这里的任何指导将不胜感激!
If you know that contact
and nextarray
are the sub-objects to merge, then:如果您知道
contact
和nextarray
是要合并的子对象,那么:
jq '. + .contact + .nextarray | del(.contact) | del(.nextarray)' file.json
{
"id": 1,
"feed": "jqQuestion",
"organization": "question.com",
"category": "question",
"website": "https://thankyou.com/",
"schedule": "daily",
"stamp_added": "2017-09-27 00:00:00",
"stamp_updated": "2022-09-10 15:51:09",
"stamp_pulled": "2022-09-10 08:51:03",
"stamp_modified": "2019-08-07 18:29:00",
"name": "",
"email": "",
"phone": "",
"value": 1,
"value2": 2
}
The +
operator, when given object operands, will merge the objects. +
运算符,当给定 object 操作数时,将合并对象。
Generically:一般:
jq '
to_entries
| map(if (.value|type) == "object" then .value else {(.key): .value} end)
| add
'
{
"id": 1,
"feed": "jqQuestion",
"organization": "question.com",
"category": "question",
"name": "",
"email": "",
"phone": "",
"website": "https://thankyou.com/",
"schedule": "daily",
"stamp_added": "2017-09-27 00:00:00",
"stamp_updated": "2022-09-10 15:51:09",
"stamp_pulled": "2022-09-10 08:51:03",
"stamp_modified": "2019-08-07 18:29:00",
"value": 1,
"value2": 2
}
jq -c 'to_entries[]|flatten[]'
would already get you to已经让你
"id"
1
"feed"
"jqQuestion"
"organization"
"question.com"
"category"
"question"
"contact"
{"name":"","email":"","phone":""}
"website"
"https://thankyou.com/"
"schedule"
"daily"
"stamp_added"
"2017-09-27 00:00:00"
"stamp_updated"
"2022-09-10 15:51:09"
"stamp_pulled"
"2022-09-10 08:51:03"
"stamp_modified"
"2019-08-07 18:29:00"
"nextarray"
{"value":1,"value2":2}
to create ur desired output should be trivial from here从这里创建你想要的 output 应该是微不足道的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.