【问题标题】:JMESPath how to write a query with multi-level filter?JMESPath 如何编写带有多级过滤器的查询?
【发布时间】:2021-05-17 06:51:45
【问题描述】:

我一直在研究 JMESPath 的官方文档和一些其他资源。但是我没有成功完成以下任务:

我的数据结构是来自 vimeo api(视频列表)的 json: 数据数组包含很多对象,每个对象都是上传的文件,有很多属性和各种选项。

    "data": [
        {
          "uri": "/videos/00001",
          "name": "Video will be added.mp4",
          "description": null,
          "type": "video",
          "link": "https://vimeo.com/00001",
          "duration": 9,
          "files":[
             {
              "quality": "hd",
              "type": "video/mp4",
              "width": 1440,
              "height": 1440,
              "link": "https://player.vimeo.com/external/4443333.sd.mp4",
              "created_time": "2020-09-01T19:10:01+00:00",
              "fps": 30,
              "size": 10807854,
              "md5": "643d9f18e0a63e0630da4ad85eecc7cb",
              "public_name": "UHD 1440p",
              "size_short": "10.31MB"
            },
            {
              "quality": "sd",
              "type": "video/mp4",
              "width": 540,
              "height": 540,
              "link": "https://player.vimeo.com/external/44444444.sd.mp4",
              "created_time": "2020-09-01T19:10:01+00:00",
              "fps": 30,
              "size": 1345793,
              "md5": "cb568939bb7b276eb468d9474c1f63f6",
              "public_name": "SD 540p",
              "size_short": "1.28MB"
            },
            ... other data
          ]
        },
   ... other uploaded files
]

我需要应用的过滤器是持续时间需要小于10,文件宽度需要为540,结果需要包含来自文件的链接(url)

我已经设法使只有一个结构级别正常工作: data[].files[?width == '540'].link

我需要提取这种列表

[
 {
 "uri": "/videos/111111",
 "link": "https://player.vimeo.com/external/4123112312.sd.mp4"
 },
 {
 "uri": "/videos/22222",
 "link": "https://player.vimeo.com/external/1231231231.sd.mp4"
 },
...other data
]

【问题讨论】:

    标签: json html5-video vimeo vimeo-api jmespath


    【解决方案1】:

    由于持续时间在您的 data 数组中,因此您必须在该级别添加此过滤器。

    您还必须使用filtering and selecting nested data 部分中描述的内容,因为您只关心files 数组下的一种特定类型的文件,因此,您可以在| [0] 中使用相同类型的查询结构为了只提取过滤后的files 数组的第一个元素。

    因此,在您的简化示例中,查询:

    data[?duration < `10`].{ uri: uri, link: files[?width == `540`].link | [0] }
    

    会产生预期的结果:

    [
      {
        "uri": "/videos/00001",
        "link": "https://player.vimeo.com/external/44444444.sd.mp4"
      }
    ]
    

    【讨论】:

    • 非常感谢!我在jsoneditoronline.org 使用了这个过滤器,并且非常快速地获得了我的数据
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-31
    • 2016-07-17
    • 2016-10-23
    • 1970-01-01
    相关资源
    最近更新 更多