使用JsonPath解析json数据

  • Post author:
  • Post category:其他




使用JsonPath解析json数据



JsonPath介绍

JsonPath是从JSON文档中抽取指定的信息的工具。提供多种语言实现版本,包括JavaScript,Python,PHP,java。

JsonPath对于JSON,就好比xpath对于XML。

JsonPath下载地址:

JsonPath下载地址

JsonPath安装:

pip install jsonpath



JsonPath语法与XPath语法对比

Json结构清晰,可读性高,复杂度低,非常容易匹配;

JsonPath中的“根成员对象”始终称为$,无论是对象还是数组;

JsonPath表达式可以使用点表示法:

$.store.book [0].title

也可以使用[’ ‘]:

$['store']['book'][0]['title']



jsonpath操作符

操作 说明
$ 根节点,这将启动所有路径表达式;$…*(匹配所有数据)
@ 现行节点;$…book[(@.length-1)](最后一本书)
* 通配符,匹配所有元素节点;$.store.book[*].author(store中的所有的book作者)
深层扫描。 必要时在任何地方可以使用名称。就是不管位置,选择所有复合条件的条件;$…author(匹配所有作者)
. 点,表示子节点
[’’ (, ‘’)] 括号表示子项
[ (, )] 数组索引或索引
[start:end] 数组切片操作 ;$…book[0,1]
[?()] 过滤表达式,表达式必须求值为一个布尔值; $…book[?(@.isbn)]



jsonpath使用示例1

import json
from jsonpath import jsonpath

if __name__ == '__main__':
    dict = {"store": {
        "book": [
            {"category": "reference",
             "author": "Nigel Rees",
             "title": "Sayings of the Century",
             "price": 8.95
             },
            {"category": "fiction",
             "author": "Evelyn Waugh",
             "title": "Sword of Honour",
             "price": 12.99
             },
            {"category": "fiction",
             "author": "Herman Melville",
             "title": "Moby Dick",
             "isbn": "0-553-21311-3",
             "price": 8.99
             },
            {"category": "fiction",
             "author": "J. R. R. Tolkien",
             "title": "The Lord of the Rings",
             "isbn": "0-395-19395-8",
             "price": 22.99
             }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    }
    }

    # 所有的作者
    print(jsonpath(dict, '$..author'))  # 输出 ['Nigel Rees', 'Evelyn Waugh', 'Herman Melville', 'J. R. R. Tolkien']

    # store中的所有的book作者
    print(jsonpath(dict,
                   '$.store.book[*].author'))  # 输出 ['Nigel Rees', 'Evelyn Waugh', 'Herman Melville', 'J. R. R. Tolkien']

    # store下的book下的所有子元素元素
    print(jsonpath(dict,
                   '$.store.book[*]'))  # 输出 [{'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95}, {'category': 'fiction', 'author': 'Evelyn Waugh', 'title': 'Sword of Honour', 'price': 12.99}, {'category': 'fiction', 'author': 'Herman Melville', 'title': 'Moby Dick', 'isbn': '0-553-21311-3', 'price': 8.99}, {'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}]

    # store下的所有子元素元素
    print(jsonpath(dict, '$.store.*'))  # 输出 [{'teacher_id': '1', 'name': 'anne', 'sex': 'female', 'age': 32}]

    # store中的所有价钱
    print(jsonpath(dict, '$.store..price'))  # 输出  [8.95, 12.99, 8.99, 22.99, 19.95]

    # 第三本书
    print(jsonpath(dict, '$..book[2]'))  # 输出 [{'category': 'fiction', 'author': 'Herman Melville', 'title': 'Moby Dick', 'isbn': '0-553-21311-3', 'price': 8.99}]
    print(jsonpath(dict, '$...book[2]'))  # 输出 [{'category': 'fiction', 'author': 'Herman Melville', 'title': 'Moby Dick', 'isbn': '0-553-21311-3', 'price': 8.99}]

    # 最后一本书
    print(jsonpath(dict, '$...book[(@.length-1)]'))  # 输出 [{'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}]

    # 取前两本书
    print(jsonpath(dict, '$...book[0,1]'))  # 输出 [{'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95}, {'category': 'fiction', 'author': 'Evelyn Waugh', 'title': 'Sword of Honour', 'price': 12.99}]

    # 获取有jsbn的所有数
    print(jsonpath(dict, '$...book[?(@.isbn)]'))  # 输出 [{'category': 'fiction', 'author': 'Herman Melville', 'title': 'Moby Dick', 'isbn': '0-553-21311-3', 'price': 8.99}, {'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}]

    # 获取价格大于10
    print(jsonpath(dict, '$...book[?(@.price>10)]'))  # 输出 [{'category': 'fiction', 'author': 'Evelyn Waugh', 'title': 'Sword of Honour', 'price': 12.99}, {'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}]

    # 匹配所有数据
    print(jsonpath(dict, '$...*'))  # 输出



使用示例2


$

是查找的根节点,传参数是python的dict 类型,当查找到的时候返回一个list结果,查找失败的时候返回 False。

import jsonpath
result = {
    "code": 0,
    "data": [
        {
            "age": 20,
            "create_time": "2019-09-15",
            "id": 1,
            "mail": "283340479@qq.com",
            "name": "yoyo",
            "sex": "M"
        },
        {
            "age": 21,
            "create_time": "2019-09-16",
            "id": 2,
            "mail": "123445@qq.com",
            "name": "yoyo111",
            "sex": "M"
        }
    ],
    "msg": "success!"
}

msg = jsonpath.jsonpath(result, '$.msg')
print(msg)   # 输出结果 ['success!']
names = jsonpath.jsonpath(result, '$..name')
print(names)   # 输出结果 ['yoyo', 'yoyo111']
no = jsonpath.jsonpath(result, '$..yoyo')
print(no)   # 找不到是结果是 False

转载请注明:https://blog.csdn.net/Owen_goodman/article/details/115869799



版权声明:本文为Owen_goodman原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。