Home > Backend Development > Python Tutorial > How to use JsonPath for Python Json read and write operations

How to use JsonPath for Python Json read and write operations

PHPz
Release: 2023-04-18 16:43:05
forward
1152 people have browsed it

    Python Json read and write operations_Detailed explanation of JsonPath usage

    1. Introduction

    JSONPath is an information extraction class library, which is derived from A tool for extracting specified information from JSON documents, providing multiple language implementation versions, including Javascript, Python, PHP and Java.

    The installation method of JSONPath is as follows: pip install jsonpath

    Comparing JSONPath syntax and XPATH syntax, JSON has a clear structure, high readability, low complexity, and is very easy to match. . The syntax of JSONPath is similar to XPath. The following table shows the syntax comparison between JSONPath and XPath:

    How to use JsonPath for Python Json read and write operations

    2. Code example

    bookJson = {
      "store": {
        "book":[
          { "category": "reference",
            "author": "Nigel Rees",
            "title": "Sayings of the Century",
            "price": 8.95
          },
          { "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
        }
      }
    }
    Copy after login

    The variable bookJson is already included This JSON string can be deserialized by the following code to obtain a JSON object:

    books=json.loads(bookJson)
    Copy after login

    1) View the color attribute of the bicycle under the store:

    checkurl = "$.store.bicycel.color"
    print(jsonpath.jsonpath(books, checkurl))
    # 输出:['red']
    Copy after login

    2) Output the content contained in the book node All objects:

    checkurl = "$.store.book[*]"
    object_list=jsonpath.jsonpath(books, checkurl)
    print(object_list)
    Copy after login

    3) Output the first object of the book node:

    checkurl = "$.store.book[0]"
    obj = jsonpath.jsonpath(books, checkurl)
    print(obj)
    # 输出: ['category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95}]
    Copy after login

    4) Output the attribute title values ​​corresponding to all objects in the book node:

    checkurl = "$.store.book[*].title"
    titles = jsonpath.jsonpath(books, checkurl)
    print(titles)
    # 输出: ['Sayings of the Century', 'The Lord of the Rings']
    Copy after login

    5 ) Output all objects in the book node whose category is fiction:

    checkurl = "$.store.book[?(@.category=='fiction')]”
    books=jsonpath.jsonpath(books, checkurl)
    print(books)
    # 输出:[{'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lordof the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}]
    Copy after login

    6) Output all objects in the book node whose price is less than 10:

    checkurl="$.store.book[?(@.price<10)]"
    books = jsonpath.jsonpath(books, checkurl)
    print(books)
    # 输出: [{&#39;category&#39;: &#39;reference&#39;, &#39;author&#39;: &#39;Nigel Rees&#39;, &#39;title&#39;:&#39;Sayings of the Century&#39;, &#39;price&#39;: 8.95}]
    Copy after login

    7) Output all objects in the book node that contain isb :

    checkurl = "$.store.book[?(@.isb)]"
    books = jsonpath.jsonpath(books,checkurl)
    print(books)
    # 输出: [{&#39;category&#39;: &#39;fiction&#39;, &#39;author&#39;: &#39;J. R. R. Tolkien&#39;, &#39;title&#39;: &#39;The Lord of the Rings&#39;, &#39;isbn&#39;: &#39;0-395-19395-8&#39;, &#39;price&#39;: 22.99}]
    Copy after login

    The above is the detailed content of How to use JsonPath for Python Json read and write operations. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template