JMESPath 如何用多级过滤器编写查询?

问题描述

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

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

ERROR in dist/my-project/lib/components/my-component/my-component.component.d.ts:5:22 - error TS2420: Class 'MyComponent' incorrectly implements interface 'OnChanges'.
  Property 'ngOnChanges' is missing in type 'MyComponent' but required in type 'OnChanges'.

我需要应用的过滤器是持续时间需要小于 "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","width": 540,"height": 540,"link": "https://player.vimeo.com/external/44444444.sd.mp4","size": 1345793,"md5": "cb568939bb7b276eb468d9474c1f63f6","public_name": "SD 540p","size_short": "1.28MB" },... other data ] },... other uploaded files ] 并且文件宽度需要为 10 并且结果需要包含来自文件的链接(url)

我已经设法让结构级别中的一个正常工作: 540

我需要提取这种列表

data[].files[?width == '540'].link

解决方法

由于持续时间在您的 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"
  }
]

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...