比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

猜你喜欢

转载自www.cnblogs.com/rongfengliang/p/9568390.html