比jsonpath 更方便的json 数据查询JMESPath 使用
类似xml 的xpath json 有jsonpath 都是为了方便进行数据查询,但是jsonpath 的功能
并不是很强大,如果为了方便查询可以使用jmespath。
以下为简单使用:
查询格式
search(<jmespath expr>, <JSON document>) -> <return value>
基本使用
代码使用nodejs jmespath 包,但是这个包有点问题,没有按照规范的格式编写api
- 安装包
yarn init -y
yarn add jmespath
package.json
{
"name": "jmespath",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"jmespath": "^0.15.0"
},
"scripts": {
"start":"node app"
}
}
- 查询数据结构(最简单例子)
const jmespath= require("jmespath");
const result = jmespath.search({
a:"dalong"
},"a")
console.log(result)
输出结果:
yarn start
dalong
支持的复杂查询格式
- 数组查询
const result2 = jmespath.search({
a:"dalong",
userids:[1,4,5,6]
},"userids[0]")
console.log(`array index 0 ${result2}`)
- 数组切片(负数可以支持倒序)
const result3 = jmespath.search({
a:"dalong",
userids:[1,4,5,6]
},"userids[0:2]")
console.log(`array slice 0:2 ${result3}`)
- 投影(支持list object slice flattern filter)
const result4 = jmespath.search({
a:"dalong",
userids:[1,4,5,6],
people: [
{"first": "James", "last": "d"},
{"first": "Jacob", "last": "e"},
{"first": "Jayden", "last": "f"},
{"missing": "different"}
]
},"people[*].first")
console.log(`array slice 0:2 ${JSON.stringify(result4)}`)
const result5 = jmespath.search({
a:"dalong",
userids:[1,4,5,6],
people: [
{"first": "James", "last": "d","age":14},
{"first": "Jacob", "last": "e","age":40},
{"first": "Jayden", "last": "f"},
{"missing": "different"}
]
},"people[?age>'10'].first")
console.log(`array filter ${JSON.stringify(result5)}`)
- mutilselect
const result5 = jmespath.search({
a:"dalong",
userids:[1,4,5,6],
people: [
{"first": "James", "last": "d","age":90},
{"first": "Jacob", "last": "e","age":40},
{"first": "Jayden", "last": "f"},
{"missing": "different"}
]
},"people[?age>'10'][first,age]")
console.log(`array filter ${JSON.stringify(result5)}`)
- pipe 查询(| 操作)
const result6 = jmespath.search({
a:"dalong",
userids:[1,4,5,6],
people: [
{"first": "James", "last": "d","age":90},
{"first": "Jacob", "last": "e","age":40},
{"first": "Jayden", "last": "f"},
{"missing": "different"}
]
},"people[?age>'10'][first,age]| [0] | [0]")
console.log(`array filter ${JSON.stringify(result6)}`)
- function
length
const result7 = jmespath.search({
a:"dalong",
userids:[1,4,5,6],
people: [
{"first": "James", "last": "d","age":90},
{"first": "Jacob", "last": "e","age":40},
{"first": "Jayden", "last": "f"},
{"missing": "different"}
]
},"length(people)")
console.log(`array filter ${JSON.stringify(result7)}`)
max_by
const result8 = jmespath.search({
a:"dalong",
userids:[1,4,5,6],
people: [
{"first": "James", "last": "d","age":90},
{"first": "Jacob", "last": "e","age":40},
{"first": "Jayden", "last": "f","age":33},
{"missing": "different","age":55}
]
},"max_by(people,&age).first")
console.log(`array filter ${JSON.stringify(result8)}`)
参考资料
http://jmespath.org/specification.html#functions
http://jmespath.org/tutorial.html
https://www.npmjs.com/package/jmespath
https://github.com/rongfengliang/jmespath-demo
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2014-08-31 asp.net MVC 使用wifidog 协议实现wifi认证