PYTHON3:JSONPATH-RW working with JSON objects

Source: Internet
Author: User

Prerequisite: In the interface Automation test, there is a dependency: test_02 the value of a request parameter, you need to rely on test_01 to return the data of a field in the result, so we need to get the value of the specific field in the return data first. Use this to Python in JSONPATH-RW library

1. Download and install

Pip Install JSONPATH-RW

2. Import

From JSONPATH_RW import Jsonpath,parse

3. Example Introduction

1. The match data is returned, but we want the value data

jsonpath_expr = Parse ('Foo[*].baz') Data= {'Foo': [{'Baz':'News'}, {'Baz':'Music'}]}Print([Match forMatchinchjsonpath_expr.find (data)]) Run Result: [Datumincontext (Value='News', Path=fields ('Baz'), Context=datumincontext (value={'Baz':'News'}, Path=<jsonpath_rw.jsonpath.index object at 0x025ca850>, Context=datumincontext (value=[{'Baz':'News'}, {'Baz':'Music'}], Path=fields ('Foo'), Context=datumincontext (value={'Foo': [{'Baz':'News'}, {'Baz':'Music'}]}, Path=this (), Context=none))), Datumincontext (value='Music', Path=fields ('Baz'), Context=datumincontext (value={'Baz':'Music'}, Path=<jsonpath_rw.jsonpath.index object at 0x025ca770>, Context=datumincontext (value=[{'Baz':'News'}, {'Baz':'Music'}], Path=fields ('Foo'), Context=datumincontext (value={'Foo': [{'Baz':'News'}, {'Baz':'Music'}]}, Path=this (), Context=none)))]

2. Get the matching data match.value

jsonpath_expr = Parse ('Foo[*].baz') Data= {'Foo': [{'Baz':'News'}, {'Baz':'Music'}]}Print([Match.value forMatchinchJsonpath_expr.find (data)])
Operation Result:
[' News ', ' music ']

3.Match.value return data is a list, we want to get a specific value

jsonpath_expr = Parse ('Foo[*].baz') Data= {'Foo': [{'Baz':'News'}, {'Baz':'Music'}]}Print([Match.value forMatchinchjsonpath_expr.find (data)] [0]) Running results: News

PYTHON3:JSONPATH-RW working with JSON objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.