在本应很简单的任务中使用JQ时,我遇到了问题。
下面是一个JSON示例:
[
{
"title": "channel 1",
"url": "rtsp://thelink"
},
{
"title": "channel UFO",
"url": "rtsp://thatlink"
},
{
"title": "channel oreo",
"url": "rtsp://thatotherlink"
},
{
"title": "channel blabla",
"url": "rtsp://yetanotherlink"
},
{
"title": "channel potato",
"url": "rtsp://anotherlinkwhatnow"
}
]
我正在尝试将数组展平为更大的数组,以便稍后在低功耗设备上更容易解析。下面是它应该是什么样子:
{
"channel 1": "rtsp://thelink",
"channel UFO": "rtsp://thatlink",
"channel oreo": "rtsp://thatotherlink",
"channel blabla": "rtsp://yetanotherlink",
"channel potato": "rtsp://anotherlinkwhatnow"
}
我甚至不会假装理解JSON工作原理的完整结构,但随着这类小问题的解决,我更接近理解了。
感谢您的帮助。顺便说一句,精通PHP,我可以非常简单地使用foreach
迭代,并用每个值作为键重新创建新的值。但我真的希望更好地了解JQ是如何工作的(并不是因为没有尝试或阅读手册!)。
发布于 2020-10-10 07:50:18
使用map
根据您感兴趣的字段将对象数组映射到键-值对,然后使用add
将这些对合并到一个对象中。
$ jq 'map({(.title): .url}) | add' test.json
{
"channel 1": "rtsp://thelink",
"channel UFO": "rtsp://thatlink",
"channel oreo": "rtsp://thatotherlink",
"channel blabla": "rtsp://yetanotherlink",
"channel potato": "rtsp://anotherlinkwhatnow"
}
https://stackoverflow.com/questions/64288520
复制相似问题