Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the jsonpath extractor in python

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Most people do not understand the knowledge points of this article "how to use the jsonpath extractor in python", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use the jsonpath extractor in python" article.

Why use jsonpath?

Just like why you use xpath, jsonpath is inspired by xpath. A powerful json data extraction tool. Allows users to extract the corresponding json data without writing a script.

Syntax of jsonpath

What can jsonpath do to retrieve data in two modes:

Separated by points

$.store.book [0] .title

$.store.book [*] .title # can get the book value

Separated by square brackets

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

For input. Path, the inner middle path will always use the more generic bracket pattern. (I guess because jsonpath is dict in python, the access method happens to be in square brackets.)

[start:end:step] mode is also supported

@ symbolic expression: can be used to represent either length or name.

$.store.book [(@ .length-1)] .title # gets the title of the last book

$.store.book [? (@ .price < 10)] .title # fetches the title of a book whose price is less than 10

Jsonpath parsing

Next, let's talk about a very powerful and convenient jsonpath dedicated to json parsing to solve the long-standing problem of deep path!

Install the dependency package first

Pip install jsonpath

To learn jsonpath, we have to mention xpath, and the grammar between the two is similar.

XpathJSONPath description / $follows node. @ current node /. Or [] takes the child node.. nCompact a takes the parent node JsonPath does not support / /.. The relative node is to select all the conditions that meet the conditions regardless of the location.

| | * | match all element nodes |

[] | [] | iterator tag (you can do simple iterative operations, such as array subscript, select values according to content, etc.)

& # 124 | [,] | support multiple selections in iterators

[] |? () | filter operation is supported

Ncanapa | () | support expression evaluation

() | Nampa | grouping, which is not supported by JsonPath

Use the example

$is the root node of the lookup, and the passing parameter is the dict type of python. It returns a list result when it is found, and False when the lookup fails.

Import jsonpathresult = {"code": 0, "data": [{"age": 20, "create_time": "2021-09-15", "id": 1, "mail": "2833479@qq.com", "name": "yoyo", "sex": "M"} {"age": 21, "create_time": "2021-09-16", "id": 2, "mail": "12344@qq.com", "name": "yoyo111", "sex": "M"}], "msg": "success!"} msg = jsonpath.jsonpath (result '$.msg') print (msg) # output result names = jsonpath.jsonpath (result,'$.. name') print (names) # output result ['yoyo',' yoyo111'] no = jsonpath.jsonpath (result,'$.. yoyo') print (no) # No result found is False

In this way, the value can be taken regardless of the hierarchy.

The above is about the content of this article on "how to use the jsonpath extractor in python". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report