【问题标题】:merge two lists within a object with joint condition including length check合并一个对象内的两个列表,包括长度检查
【发布时间】: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


【解决方案1】:

这是一个帮助您入门的函数。您可以扩展它以满足您的需求。

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

Try it online!

【讨论】:

  • 谢谢我试试。似乎值得研究它以进一步了解jq
  • 我尝试使用索引如下:'INDEX(.image_files[]; .id) as $imgs | [ .annotations[] | select(.attributes.type == "word") | select(.label | length | . ==4) ]' 并得到想要的结果。
  • 搜索最终答案的人; 'INDEX(.image_files[]; .id) as $imgs | [ .annotations[] | select(.attributes.type == "word") | select(.label | length | . ==4) | $imgs[.image_id] + {label:.label} ]' 重点是 jq 中的大部分内容都是过滤器。因此,您应该熟悉管道使用和每个管道阶段中的当前输入. 概念。干杯...
猜你喜欢
  • 2017-01-17
  • 2017-08-21
  • 2020-03-22
  • 1970-01-01
  • 2018-04-21
  • 2017-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多