类似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 使用的更多相关文章

  1. 8种json数据查询方式

    你有没有对“在复杂的JSON数据结构中查找匹配内容”而烦恼.这里有8种不同的方式可以做到: JsonSQL JsonSQL实现了使用SQL select语句在json数据结构中查询的功能. 例子: ? ...

  2. JSON数据查询方法

    在进行前端项目开发的时候时长会遇到JSON的数据查找问题,如何方便快速查找?这里推荐一个linqjs组件,项目主页参见http://linqjs.codeplex.com/ 查询对象 var json ...

  3. C# dynamic类型序列化和反序列化之Newtonsoft.Json,动态解析远端返回的jSON数据

    一.说明 1.Newtonsoft.Json 中的Linq To Json中提供了方便的json数据查询.修改等操作. 例如:JObject,JArray 2.在JObject.FromObject( ...

  4. redis存json数据时选择string还是hash

    redis存json数据时选择string还是hash 我们在缓存json数据到redis时经常会面临是选择string类型还是选择hash类型去存储.接下来我从占用空间和IO两方面来分析这两种类型的 ...

  5. Python格式化处理json数据的方式

    1.问题 在遇到json数据的过程中,我们经常需要获取json数据中某个值的操作,如果是用get方法去取比较繁琐,接下来介绍两种方式来取值. 2.jsonpath来格式化处理json数据 2.1介绍 ...

  6. Android解析中国天气接口JSon数据,应用于天气查询!

    android解析Json数据是比较常见的一种操作.也是客户端和服务器进行数据交互的桥梁.下面就来看一看在android中解析JSon数据的方法吧. 首先要想获得Json数据,就必须访问相关的网络接口 ...

  7. Python | JSON 数据解析(Json & JsonPath)

    一.什么是JSON? JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式.它基于 ECMAScript (欧洲计算机协会制定的js规范)的一 ...

  8. 使用TSQL查询和更新 JSON 数据

    JSON是一个非常流行的,用于数据交换的文本数据(textual data)格式,主要用于Web和移动应用程序中.JSON 使用“键/值对”(Key:Value pair)存储数据,能够表示嵌套键值对 ...

  9. Atitit.列表页面and条件查询的实现最佳实践(1)------设置查询条件and提交查询and返回json数据

    Atitit.列表页面and条件查询的实现最佳实践(1)------设置查询条件and提交查询and返回json数据 1. 1. 配置条件字段@Conditional 1 1 2. 2. 配置条件字段 ...

随机推荐

  1. 12月8日 周五 image_tag.

    Overview of helpers provided by Action View 6.1 AssetTagHelper:用于generate html语言 image_tag ,return a ...

  2. python-day12--函数进阶

    1.命名空间: 分三种:全局命名空间,局部命名空间,内置命名空间. 加载顺序:内置命名空间→全局命名空间→局部命名空间 取值顺序:局部命名空间→全局命名空间→内置命名空间 2.作用域: 作用域就是作用 ...

  3. Leetcode 106

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  4. ASP.NET简介

    1.什么是ASP.NET? ASP.NET是一套免费的网络架构,是为了构建一个伟大的或者说非常不错的网站或网络应用,并同时使用了一些前端技术,比如说HTML,CSS和JavaScript ASP.NE ...

  5. Hibernate---hbm2ddl和数据库方言的配置

    Hibernate---hbm2ddl和数据库方言的配置 hibernate配置文件--hbm2ddl.auto属性值的区别: update: 最常用的取值,如果但其数据库中不存在表结构,那么自动创建 ...

  6. python中的注释,输入输出和编码及文件

    1.单行注释 以井号( # )开头,右边的所有内容当做说明2.多行注释 以三对单引号(’’’注释内容’’’)将注释包含起来以‘# ’是注释的标识符,可以记录当前代码所代表的意义,解释器会自动忽略这部分 ...

  7. Word发布到cnblogs文章

        1◆ 打开word 2◆ 注册用户   3◆ 操作 success

  8. docker(二)不管怎么样,先跑起来。运行一个samba,运行一个hello world。

    ---恢复内容开始--- 继续上一篇文章 访问https://hub.docker.com/r/jackadam/samba/ 这是我写的一个docker镜像. 下面有 无脑测试用: docker r ...

  9. Sqoop2安装

    下载 http://www-us.apache.org/dist/sqoop/ 打开以上链接,开始下载sqoop2   下载后得到:sqoop-1.99.7-bin-hadoop200.tar.gz文 ...

  10. Beta阶段第1周/共2周 Scrum立会报告+燃尽图 05

    作业要求与 [https://edu.cnblogs.com/campus/nenu/2018fall/homework/2284] 相同 版本控制:https://git.coding.net/li ...