Merge 合并具有关节条件(包括长度检查)的对象中的两个列表

Merge 合并具有关节条件(包括长度检查)的对象中的两个列表,merge,conditional-statements,jq,Merge,Conditional Statements,Jq,大家好,jq的用户和专家们 仅供参考,这个问题与。但目前的问题是合并两个包含长度检查在内的联合条件的列表 对于json数据,如下所示: { "image_files": [ { "id": "img_0001", "width": 32, "heigt": 32, "file_name": "img_0001.png" }, { "id": "img_0002", "width": 128,

大家好,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
      }
    }
  ]
}
{
  "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.

  • annotations
    列表中的
    image\u id
    是一种外键,引用
    image\u文件
    列表中的
    id
我的问题是如何将
image\u文件
注释

  • annotations.attributes.type==“word”和
  • 注释。标签的长度==4
最终结果如下:

{
  "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
      }
    }
  ]
}
{
  "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

您的条件有些不清楚:a)注释中没有键
文本
。属性
,b)如果您的意思是
类型
的大小为4,则该条件是多余的,因为
类型==“word”
的大小始终为4。你能修改你的要求吗?@Dmitry,这是我的错。我的意思是
annotations.label
而不是
annotations.attribute.text
。我纠正了原来的问题。谢谢。我试了一下。为了进一步了解jqI在索引方面的尝试,似乎有必要对其进行研究:
索引(.image_files[].id)作为$imgs |[.annotations[]选择(.attributes.type==“word”)|选择(.label | length |.==4)]
并获得所需的结果。为谁搜索最终答案
'INDEX(.image_files[].id)作为$imgs |[.annotations[]选择(.attributes.type==“word”)|选择(.label | length |.==4)|$imgs[.image_id]+{label:.label}]
主要的一点是大部分东西都是在jq中过滤的。因此,您应该熟悉管道使用情况以及每个管道阶段中的当前输入
概念。干杯