• jmespath 的使用,类似于jsonpath


    在测试过程中,经常会取JSON中的某个值,jmespath可以是除了jsonpath的另外一种选择.
    下面通过几个例子来说明jmespath在python的使用

    jmespath python安装

    非常简单直接pip,

    pip install jmespath

    查询一个key值

    source={"a": "foo", "b": "bar", "c": "baz"}
    result = jmespath.search("a",source)
    print(result)

    这个的结果为:foo

    subexpression

    类似于jsonpath,通过.来表示路径的层级

    source_1={"a": {"b": {"c": {"d": "value"}}}}
    sub_result = jmespath.search("a.b.c",source_1)
    print(sub_result)

    这个例子的结果为:{'d': 'value'}

    index expressions

    index expression主要使用在数组上

    source_2 = ["a", "b", "c", "d", "e", "f"]
    index_result = jmespath.search("[1]",source_2)
    print(index_result)

    这个例子的结果为:b

    多个表达式综合使用

    以上几种表达式可以合起来一期使用:

    composite_exp = "a.b.c[0].d[1][0]"
    source_3= {"a": {
    "b": {
    "c": [
    {"d": [0, [1, 2]]},
    {"d": [3, 4]}
    ]
    }
    }}

    composite_result = jmespath.search(composite_exp,source_3)
    print(composite_result)

    这个例子的结果为1

    Slicing 切片

    slicing 和python本身的slicing比较像,

    source_4=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    slicing_exp = "[0:5]"
    slicing_result = jmespath.search(slicing_exp,source_4)
    print(slicing_result)

    这个例子的结果为: [0, 1, 2, 3, 4]

    slicing实际上和python自己的机制基本一样,同样这个也是主要给数组使用.
    有一点需要记住,基本的slicing的格式其实是: [start:stop:step]

    Projections

    projection不知道怎么翻译,就先叫做投影吧,具体通过例子来说比较好理解.
    projections主要包含一下几种情况:

    • List Projections
    • Slice Projections
    • Object Projections
    • Flatten Projections
    • Filter Projections

    Projections- 例子

    list_exp="people[*].first"
    source_5 = {
    "people": [
    {"first": "James", "last": "d"},
    {"first": "Jacob", "last": "e"},
    {"first": "Jayden", "last": "f"},
    {"missing": "different"}
    ],
    "foo": {"bar": "baz"}
    }

    proj_result1= jmespath.search(list_exp,source_5)
    print(proj_result1) # ['James', 'Jacob', 'Jayden']


    obj_exp ="reservations[*].instances[*].state"
    source_6= {
    "reservations": [
    {
    "instances": [
    {"state": "running"},
    {"state": "stopped"}
    ]
    },
    {
    "instances": [
    {"state": "terminated"},
    {"state": "runnning"}
    ]
    }
    ]
    }

    proj_result2=jmespath.search(obj_exp,source_6)
    print(proj_result2) #[['running', 'stopped'], ['terminated', 'runnning']]

    # Flatten projections

    source_7=[
    [0, 1],
    2,
    [3],
    4,
    [5, [6, 7]]
    ]
    flat_exp ="[]"
    flat_result = jmespath.search(flat_exp,source_7)
    print(flat_result) # [0, 1, 2, 3, 4, 5, [6, 7]]

    # filter

    filter_exp="machines[?state=='running'].name"
    filter_source ={
    "machines": [
    {"name": "a", "state": "running"},
    {"name": "b", "state": "stopped"},
    {"name": "b", "state": "running"}
    ]
    }
    filter_result = jmespath.search(filter_exp,filter_source)
    print(filter_result) # ['a','b']

    # pipe expression

    pipe_exp= "people[*].first | [0]"
    pipe_source= {
    "people": [
    {"first": "James", "last": "d"},
    {"first": "Jacob", "last": "e"},
    {"first": "Jayden", "last": "f"},
    {"missing": "different"}
    ],
    "foo": {"bar": "baz"}
    }

    pipe_result = jmespath.search(pipe_exp,pipe_source)
    print(pipe_result) # James

    # multiselect
    multi_exp="people[].[first,last]"
    multiselect_result = jmespath.search(multi_exp,pipe_source)
    print(multiselect_result) # [['James', 'd'], ['Jacob', 'e'], ['Jayden', 'f'], [None, None]]
  • 相关阅读:
    graalvm 内置require 模块的开启
    Calling out from Java to JavaScript (with call back) – leveraging interoperability support of GraalVM
    web开发 api 资源跨域的一种实践
    使用rollup 转换commonjs 模块为es6 模块,方便的支持graalvm 模块兼容
    使用json-mask 查询json 数据
    nginx njs docker 试用
    使用nginx-prometheus-exporter 监控nginx
    wso2 关于graphql 的方案
    docker也一直发展
    操作系统-容器-Docker:如何将应用打包成为 Docker 镜像?
  • 原文地址:https://www.cnblogs.com/fcc-123/p/13029799.html
Copyright © 2020-2023  润新知