有条件地合并对象中的两个列表 [英] merge two lists within a object conditionally
本文介绍了有条件地合并对象中的两个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
资深jq专家!
我是一名jq学习者,并且拥有一个json obect组成的列表,如下所示:
I'm a jq learner and have a json obect composed lists as follows:
{
"image_files": [
{
"id": "img_0001",
"width": 32,
"heigt": 32,
"file_name": "img_0001.png"
},
{
"id": "img_0002",
"width": 128,
"heigt": 32,
"file_name": "img_0002.png"
},
{
"id": "img_0003",
"width": 32,
"heigt": 32,
"file_name": "img_0003.png"
},
{
"id": "img_0004",
"width": 160,
"heigt": 32,
"file_name": "img_0004.png"
}
],
"annotations": [
{
"id": "ann_0001",
"image_id": "img_0001",
"label": "A",
"attributes": {
"type": "letter",
"augmented": false
}
},
{
"id": "ann_0002",
"image_id": "img_0002",
"label": "Good",
"attributes": {
"type": "word",
"augmented": false
}
},
{
"id": "ann_0003",
"image_id": "img_0003",
"label": "C",
"attributes": {
"type": "letter",
"augmented": false
}
},
{
"id": "ann_0004",
"image_id": "img_0004",
"label": "Hello",
"attributes": {
"type": "word",
"augmented": false
}
}
]
}
-
image_id
是引用image_files
列表中id
的外键. image_id
in theannotations
list are foreign key referencing theid
in theimage_files
list.
annotations
列表中的
我想以annotations.attribute.type == "letter"
为条件加入image_files
和annotations
.
期待以下结果:
{
"letter_image_files_with_label": [
{
"id": "img_0001",
"width": 32,
"heigt": 32,
"file_name": "img_0001.png",
"label": "A"
},
{
"id": "img_0003",
"width": 32,
"heigt": 32,
"file_name": "img_0003.png",
"label": "C"
}
]
}
如何从json数据输入中产生上述结果?
jq手册中解释的join
似乎没有使用这种任务.
有办法吗?请给我看一下绳子.
How can I produce above result from the json data input?
join
explained in jq manual does not seem to use this kind task.
Is there a way for this? Please show me the rope.
感谢您的慷慨阅读.
推荐答案
用id
索引image_files
使得这很简单.
INDEX(.image_files[]; .id) as $imgs | [
.annotations[]
| select(.attributes.type == "letter")
| $imgs[.image_id] + {label: .label}
]
这篇关于有条件地合并对象中的两个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文