比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
比jsonpath 更方便的json 数据查询JMESPath 使用的更多相关文章
- 8种json数据查询方式
你有没有对“在复杂的JSON数据结构中查找匹配内容”而烦恼.这里有8种不同的方式可以做到: JsonSQL JsonSQL实现了使用SQL select语句在json数据结构中查询的功能. 例子: ? ...
- JSON数据查询方法
在进行前端项目开发的时候时长会遇到JSON的数据查找问题,如何方便快速查找?这里推荐一个linqjs组件,项目主页参见http://linqjs.codeplex.com/ 查询对象 var json ...
- C# dynamic类型序列化和反序列化之Newtonsoft.Json,动态解析远端返回的jSON数据
一.说明 1.Newtonsoft.Json 中的Linq To Json中提供了方便的json数据查询.修改等操作. 例如:JObject,JArray 2.在JObject.FromObject( ...
- redis存json数据时选择string还是hash
redis存json数据时选择string还是hash 我们在缓存json数据到redis时经常会面临是选择string类型还是选择hash类型去存储.接下来我从占用空间和IO两方面来分析这两种类型的 ...
- Python格式化处理json数据的方式
1.问题 在遇到json数据的过程中,我们经常需要获取json数据中某个值的操作,如果是用get方法去取比较繁琐,接下来介绍两种方式来取值. 2.jsonpath来格式化处理json数据 2.1介绍 ...
- Android解析中国天气接口JSon数据,应用于天气查询!
android解析Json数据是比较常见的一种操作.也是客户端和服务器进行数据交互的桥梁.下面就来看一看在android中解析JSon数据的方法吧. 首先要想获得Json数据,就必须访问相关的网络接口 ...
- Python | JSON 数据解析(Json & JsonPath)
一.什么是JSON? JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式.它基于 ECMAScript (欧洲计算机协会制定的js规范)的一 ...
- 使用TSQL查询和更新 JSON 数据
JSON是一个非常流行的,用于数据交换的文本数据(textual data)格式,主要用于Web和移动应用程序中.JSON 使用“键/值对”(Key:Value pair)存储数据,能够表示嵌套键值对 ...
- Atitit.列表页面and条件查询的实现最佳实践(1)------设置查询条件and提交查询and返回json数据
Atitit.列表页面and条件查询的实现最佳实践(1)------设置查询条件and提交查询and返回json数据 1. 1. 配置条件字段@Conditional 1 1 2. 2. 配置条件字段 ...
随机推荐
- axios构建请求池处理全局loading状态&&axios避免重复请求
很多时候我们能够看到类似进度条一样的东西在页面顶部进行加载,代表页面是否加载完成,或者其他的loading效果,我们当然不可能通过promise.all来讲所有的请求合并到一起然后进行处理,这个时候我 ...
- mysql查询出来的某一列合并成一个字段
查询所有用户及角色,一个用户会有多个角色 select u.*,sr.* from users u left join sys_user_role sur on sur.userCode=u.id l ...
- java.lang.UnsupportedClassVersionError: com/my/test/TestUser : Unsupported major.minor version 52.0
问题原因: 1.执行代码的jdk版本 低于 编译的jdk版本 2.项目用JDK1.8运行过,现在又在本地的eclipse等开发工具或者本地环境变量为低版本的jdk1.7或者jdk1.6下运行,ecli ...
- 43 HTML CSS
第一部分: 1.meta 标签中:meta 标签中 <meta http-equvi = "refresh" content="2;URL= https://www ...
- csp公共钥匙盒
1.公共钥匙盒 问题描述 有一个学校的老师共用N个教室,按照规定,所有的钥匙都必须放在公共钥匙盒里,老师不能带钥匙回家.每次老师上课前,都从公共钥匙盒里找到自己上课的教室的钥匙去开门,上完课后,再将钥 ...
- CoderForce 180D-Name (构造+回溯)
题目大意:给两个字符串s,t,用s中的字符重新组合构造出按字典序最小的但比t大的新字符串. 题目分析:先统计s中各个字母出现的次数,然后从t的左端向右端依次构造出新串的每一位上的字母.这个过程我是用回 ...
- LeetCode 48. Rotate Image My Submissions Question (矩阵旋转)
题目大意:给一个矩阵,将其按顺时针旋转90°. 题目分析:通法是先将矩阵转置,然后再反转每一行,或者是先反转每一列,然后再将其转置.I just want to say"It's amazi ...
- Linux 强制安装rpm 包
Linux 强制安装rpm 包 2014年12月12日 10:21 [root@ilearndb1 Server]# rpm -ivh unixODBC-devel-2.* --nodeps -- ...
- C++实现Vector容器的基本功能
本文只实现了Vector的默认构造函数.赋值构造函数.赋值函数.析构函数.重置空间大小函数和插入函数,权当起到抛砖引玉的作用,其他函数功能的实现可在此基础之上进行拓展. #include <io ...
- js数组与字符串之间的相互转换
一.数组转字符串 需要将数组元素用某个字符连接成字符串,示例代码如下 <!doctype html> <html> <head> <meta charset= ...