merge - 合并一个对象中的两个列表,包括长度检查
问题描述
您好jq用户和专家,
仅供参考,这个问题与有条件地合并一个对象中的两个列表有关。但是当前的问题是与两个没有联合条件的列表合并,包括长度检查。
对于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
}
}
]
}
image_id
在annotations
列表中是一种外键引用列表id
中的image_files
。
我的问题是如何加入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.
解决方案
这是一个帮助您入门的功能。您可以扩展它以满足您的需求。
def Hongsoog:
def makemap: reduce .annotations[] as $item ({}; .[$item.image_id]=$item) ;
def join: makemap as $idx | [ .image_files[] | { i:., a: $idx[.id] } ] ;
def isword: .a.attributes.type == "word" ;
def islen4: .a.label | length == 4 ;
def format: .i.label = .a.label | .i ;
def result: [ join[] | select(isword and islen4) | format ];
{
"four_letter_word_image_files_with_label": result
};
Hongsoog
推荐阅读
- angular - 如何使用 Angular 5 识别来自 HTTP 拦截器的特定请求?
- html - 进入时从网页中抓取表格格式 - 使用 Beautiful Soup
- matlab - 如何将数据从 Arduino mkr zreo 保存到 matlab?
- javascript - 使绝对菜单响应大小的变化
- c# - 输入无效 .Net Core Web API
- php - 在提交表单时在弹出窗口中打印 php 输出
- c# - 写入文件时访问冲突
- c# - C# 类库中的 REST Web 服务
- delphi - HTTPS 帖子不起作用
- c++ - 将 uint64_t 位掩码转换为 std::array of bool