Introduction to the use of python jasonpath library

1. Introduction

The jsonn data returned by the interface needs to be asserted after taking the value. Generally, we use jsonpath to extract the data returned by the interface. JsonPath is an information extraction class library, which is a tool for extracting specified information from JSON documents. JsonPath is equivalent to XPath.

Install

pip install jsonpath

2. Grammatical description

The content parsed by JsonPath must be in dictionary format, and the content obtained through JsonPath will be returned in the form of a list. If there is an error in the expression, it will return False (a value of type Boolean)

Basic format specification for jsonpath expressions:

symbol

describe

$

Represents the root node and the start of all jsonpath expressions

.

Represents getting child nodes

..

Indicates to get all eligible content

*

Represents all element nodes

[]

Represents the label of the iterator (can be used to handle subscripts, etc.)

[,]

Represents a selection of multiple results

?()

Represents a filter operation

@

Indicates the current node

3. Code example

import jsonpath

data = { "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
    }
  }
}

# 获取所有book的价格
price = jsonpath.jsonpath(data, '$.store.book[*].price')
print(price)
>> [8.95, 12.99, 8.99, 22.99]

# 获取部分book节点下的price节点值,可支持切片操作
res1 = jsonpath.jsonpath(data, '$.store.book[0,1].price')
res2 = jsonpath.jsonpath(data, '$.store.book[0:2].price')
print(res1)
print(res2)
>> [8.95, 12.99]
>> [8.95, 12.99]

# 获取最后一本书的价格(不能通过[-1]这种方式获取)
res3 = jsonpath.jsonpath(data, '$.store.book[-1:].price')
print(res3)
>> [22.99]

# 获取store节点下所有price节点的值
res4 = jsonpath.jsonpath(data, '$.store...price')
res5 = jsonpath.jsonpath(data, '$..price')
print(res4)
print(res5)
>> [8.95, 12.99, 8.99, 22.99, 19.95]
>> [8.95, 12.99, 8.99, 22.99, 19.95]

# jsonpath解析错误,返回bool类型数据false
res6 = jsonpath.jsonpath(data, '$.store.book111')
print(res6)
>> False

# 获取价格大于10的所有书籍信息
book = jsonpath.jsonpath(data, '$..book[?(@.price>10)]')
print(book)
>> [{'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}]

# 获取book的价格大于10的prcice
price = jsonpath.jsonpath(data, '$..book[?(@.price>10)].price')
print(price)
>> [12.99, 22.99]

# 过滤所有带有 isbn编号的书籍信息
isbn = jsonpath.jsonpath(data, '$..book[?(@.isbn)]')
print(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}]

Guess you like

Origin blog.csdn.net/qq_38571773/article/details/128789523