【发布时间】:2020-04-01 12:37:23
【问题描述】:
你好jq用户和专家,
仅供参考,这个问题与merge two lists within a object conditionally 有关。但目前的问题是与两个列表合并,包括长度检查。
对于json数据如下:
{
"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
}
}
]
}
-
annotations列表中的image_id是一种外键引用image_files列表中的id。
我的问题是如何以联合条件加入image_files 和annotations
- annotations.attributes.type == "word" AND
- annotations.label' 长度 == 4。
最终结果应该如下:
{
"four_letter_word_image_files_with_label": [
{
"id": "img_0002",
"width": 128,
"heigt": 32,
"file_name": "img_0004.png"
"label": "Good"
}
]
}
How can I produce above result from the json data input?
Thanks for your reading.
【问题讨论】:
-
您的条件有点不清楚:a)
annotations.attributes中没有键text,b) 如果您的意思是type的大小为 4,那么条件是多余的,因为type == "word"总是 4 的大小。你能解决你的要求吗? -
@Dmitry,这是我的错误。我的意思是
annotations.label而不是annotations.attribute.text。我更正了原来的问题。
标签: merge conditional-statements jq