类似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月16日 增加一个购物车内product数量的功能, 自定义method,在helper中定义,计算代码Refactor到Model中。

    仿照Rails实战:购物网站 教材:5-6 step5:计算总价,做出在nav上显示购物车内product的数量. 遇到的❌: 1. <% sum = 0 %> <% current ...

  2. 『科学计算_理论』优化算法:梯度下降法&牛顿法

    梯度下降法 梯度下降法用来求解目标函数的极值.这个极值是给定模型给定数据之后在参数空间中搜索找到的.迭代过程为: 可以看出,梯度下降法更新参数的方式为目标函数在当前参数取值下的梯度值,前面再加上一个步 ...

  3. 『cs231n』计算机视觉基础

    线性分类器损失函数明细: 『cs231n』线性分类器损失函数 最优化Optimiz部分代码: 1.随机搜索 bestloss = float('inf') # 无穷大 for num in range ...

  4. ioS UI-导航控制器(NavigationController)

    #import "AppDelegate.h" #import "ViewController.h" @interface AppDelegate () @en ...

  5. 使用python,将excel数据批量导入数据库

    这是上一篇文章的优化版本,相较于一条一条的执行sql语句,本文中,将excel中所有的数据先写到list列表中 在通过函数 cursor.executemany(sql, list) 一次性写入到数据 ...

  6. 组数排序非sort

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...

  7. flashfxp 命令行

    以后更新软件时,尽量用bat命令行 http://www.flashfxp.com/forum/flashfxp/frequently-asked-questions-faq-/14748-comma ...

  8. jQuery 3.1 参考手册.CHM离线版下载

    制作了一份jQuery 3.1 参考手册.CHM离线版供大家使用 点击下载 预览一下

  9. Python int操作

    a = 1 # 1 print(a.bit_length()) #计算一个数字的二进制长度.没啥用

  10. ThinkPHP CodeIgniter URL访问举例

      ThinkPHP   URL访问:   http://localhost/think/index.php/Home/login/func/[name/syt/password/123/]   ht ...