如何用JMESPath拆分JSON中file_location并访问第三个元素?
如何用JMESPath访问file_location拆分后的指定元素?
给定如下JSON结构:
{ "year": "2022", "cycle": "Aug", "origin": { "file_location": "GDT Post-Distribution Manifest/Italy/Rome/2022/Aug/Italy Rome_202208241528.xlsx", "protocol": "s3:" } }
要实现将origin.file_location按斜杠/拆分后取第三个元素,并验证其是否等于指定值,可使用以下JMESPath表达式:
1. 直接获取第三个元素
split(origin.file_location, '/')[2]
split(origin.file_location, '/'):把file_location字符串按/分割成数组,拆分后的数组为:["GDT Post-Distribution Manifest", "Italy", "Rome", "2022", "Aug", "Italy Rome_202208241528.xlsx"][2]:JMESPath数组索引从0开始,第三个元素对应索引2,因此取到的值为Rome。
2. 验证元素是否等于指定值
如果要直接检查该元素是否等于目标值(比如Rome),可以用比较表达式:
split(origin.file_location, '/')[2] == 'Rome'
执行后会返回布尔值true或false,用于判断是否匹配。
内容的提问来源于stack exchange,提问作者user2772056
相关产品推荐
相关产品推荐

