gpt4 book ai didi

json - JMESPath 如何编写带有多级过滤器的查询?

转载 作者:行者123 更新时间:2023-12-05 06:04:51 29 4
gpt4 key购买 nike

我一直在研究 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
]

最佳答案

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

关于json - JMESPath 如何编写带有多级过滤器的查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66190176/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com