拥有以下json输出:
[
{
"id": "47",
"canUpdate": true,
"canDelete": true,
"canArchive": true,
"info": [
{
"key": "problem_type",
"value": "PAN",
"valueCaption": "PAN",
"keyCaption": "Category"
},
{
"key": "status",
"value": 3,
"valueCaption": "Closed",
"keyCaption": "Status"
},
{
"key": "insert_time",
"value": 1466446314000,
"valueCaption": "2016-06-20 14:11:54.0",
"keyCaption": "Request time"
}
正如您在" info"他们实际上将键:值对标记为"键":" problem_type"和"价值":" PAN"然后" valueCaption":" PAN" " keyCaption":"类别"。我需要做的是重新映射文件,以便在此示例中,它显示为" problem_type":" PAN" "类别":" PAN"。以这种方式迭代输出以重新映射键:值对的最佳方法是什么?
需要如何:
[
{
"id": "47",
"canUpdate": true,
"canDelete": true,
"canArchive": true,
"info": [
{
"problem_type": "PAN",
"Category": "PAN"
},
{
"status": 3,
"Status": "Closed"
},
{
"insert_time": 1466446314000,
"Request time": "2016-06-20 14:11:54.0"
}
答案 0 :(得分:0)
以下是使用jq
的Update assignment |=解决方案.[].info[] |= {(.key):.value, (.keyCaption):.valueCaption}
示例运行(假设data.json
中的数据)
$ jq -M '.[].info[] |= {(.key):.value, (.keyCaption):.valueCaption}' data.json
[
{
"id": "47",
"canUpdate": true,
"canDelete": true,
"canArchive": true,
"info": [
{
"problem_type": "PAN",
"Category": "PAN"
},
{
"status": 3,
"Status": "Closed"
},
{
"insert_time": 1466446314000,
"Request time": "2016-06-20 14:11:54.0"
}
]
}
]