• 比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

  • 相关阅读:
    fork和Vfork的区别
    exer4.13.c(undone)
    Exer4.6.c(undone)
    好习惯
    c语言中的register修饰符
    请教如何在页面之间传递dataSet?不用session
    ultraEdite编辑shell或perl程序时注意
    PowerBuilder程序中取数据库中值,值异常(正数变成负数或异常)
    pb程序的编译发布
    关于sql server2000 的1068 与 1069 问题
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/9568390.html
Copyright © 2020-2023  润新知