我如何从这个转换(使用jq
)下面的 * json * 结构:
{
"_internal_messages": {
"error": [
{
"date": "16:12:30 - 07/02/2023",
"id": 1,
"origin": "A",
"text": "This is an error message"
},
{
"date": "16:12:31 - 07/02/2023",
"id": 5,
"origin": "A",
"text": "This is a second error message"
}
],
"info": [
{
"date": "16:12:29 - 07/02/2023",
"id": 0,
"origin": "A",
"text": "This is an info message"
},
{
"date": "16:12:30 - 07/02/2023",
"id": 4,
"origin": "C",
"text": "This is a second info message"
}
],
"success": [
{
"date": "16:12:30 - 07/02/2023",
"id": 2,
"origin": "B",
"text": "This is a success message"
},
{
"date": "16:12:30 - 07/02/2023",
"id": 3,
"origin": "B",
"text": "This is a second success message"
},
{
"date": "16:12:31 - 07/02/2023",
"id": 6,
"origin": "C",
"text": "This is a third success message"
}
]
}
}
对此:
{"_internal_messages":[
{
"type":"info",
"date": "16:12:29 - 07/02/2023",
"id": 0,
"origin": "A",
"text": "This is an info message"
},{
"type":"error",
"date": "16:12:30 - 07/02/2023",
"id": 1,
"origin": "A",
"text": "This is an error message"
},{
"type":"success",
"date": "16:12:30 - 07/02/2023",
"id": 2,
"origin": "B",
"text": "This is a success message"
},{
"type":"success",
"date": "16:12:30 - 07/02/2023",
"id": 3,
"origin": "B",
"text": "This is a second success message"
},{
"type":"info",
"date": "16:12:30 - 07/02/2023",
"id": 4,
"origin": "C",
"text": "This is a second info message"
},{
"type":"error",
"date": "16:12:31 - 07/02/2023",
"id": 5,
"origin": "A",
"text": "This is a second error message"
},{
"type":"success",
"date": "16:12:31 - 07/02/2023",
"id": 6,
"origin": "C",
"text": "This is a third success message"
}
]}
我检查了 * jq手册 * 和之前在SO回答的一些问题,但是我不能破解这个问题...我在考虑结合jq
和bash
来做,但是我确信一定有一个更好的方法只使用jq
,但是我的技能还没有。有人能帮我吗?谢谢!
在原始结构中,消息存储在3个数组中:error
、info
和success
。每次向这些数组中添加消息时,它都会收到一个连续的/增量的id
。我想要的是从这三个数组中取出所有这些消息,将它们放在一个数组中,同时添加一个额外的属性以了解每个消息来自何处(error
、info
或success
),最后根据它们的id按升序对其进行排序。棘手的是,一旦将它们放在一起,就知道哪一个是error
、info
或success
。
1条答案
按热度按时间flseospp1#
我不明白为什么在您想要的输出中缺少其中一项,但除此之外,它通过将键Map到另一个字段来展平对象:
如果要按
.id
对项目进行排序,请附加sort_by
:一个二个一个一个