python-jsonpath parsing artifact

Foreword

jsonpath is used to parse json data. It is a simple way to extract part of the content of a given JSON document. JsonPath has many programming languages such as Javascript, Python and PHP, Java.

The json parsing provided by JsonPath is very powerful. It provides a regular expression-like grammar, which can basically satisfy all the json content you want. Below I use code to implement each expression introduced on the official website, so that you can more intuitively know how to use it.

Environmental installation

jsonpath is a third-party module, want to install additional

pip install jsonpath

jsonpath syntax rules

Xpath JSONPath Description
/ $ Follow node
. @ Current node
/ . or [] take child node
.. n/a Fetching the parent node JsonPath does not support
// .. Relative node is to select all the conditions that meet the conditions regardless of the location
* * Match all element nodes
[] [] Iterator mark (you can do simple iterative operations in it, such as array subscripts, select values based on content, etc.)
[,]
[] ?() Support filtering operation
n/a () Support expression calculation
() n/a Grouping, JsonPath does not support

jsonpath usage example

"""
Create by dell on 2020/10/4
Author: wencheng
WeChat public: automated testing To share
"""
import jsonpath

jsonobj ={"code":0,"msg":"success","data":{"output":[{"id":"867899031136606","prediction":0.698744767973,"instances":[]},{"id":"867899031133728","prediction":0.695468634754,"instances":[]}]}}
msg = jsonpath.jsonpath(jsonobj,'$.msg')print("msg:%s"% msg)print("================================")

data = jsonpath.jsonpath(jsonobj,'$.data')print("data:%s"% data)print("================================")

output = jsonpath.jsonpath(jsonobj,'$..output')print("output:%s"% output)print("================================")
id = jsonpath.jsonpath(jsonobj,'$...id')print("id:%s"% id)

Results of the:

C:\python3.6\python.exe E:/Gitwokspace/Daily_py/Fileprocessing/test/jsonpath11.py

msg:['success']

================================

data:[{'output': [{'id': '867899031136606', 'prediction': 0.698744767973, 'instances': []}, {'id': '867899031133728', 'prediction': 0.695468634754, 'instances': []}]}]

================================

output:[[{'id': '867899031136606', 'prediction': 0.698744767973, 'instances': []}, {'id': '867899031133728', 'prediction': 0.695468634754, 'instances': []}]]

================================

id:['867899031136606', '867899031133728']

Process finished with exit code 0

Here are some more practical json online tools for everyone to recommend for your reference

Online JSON code inspection, inspection, beautification and formatting tool: http://tools.jb51.net/code/json

JSON online formatting tool: http://tools.jb51.net/code/jsonformat

Online XML/JSON conversion tool: http://tools.jb51.net/code/xmljson

json code online formatting/beautification/compression/editing/conversion tool: http://tools.jb51.net/code/jsoncodeformat

Online json compression/escaping tool:
http://tools.jb51.net/code/json_yasuo_trans

The above is the entire content of this article. I hope that the content of this article has a certain reference learning value for everyone's study or work. If you have any questions, you can leave a message and exchange. Thank you for your support.

Recommended Posts

python-jsonpath parsing artifact