Use of JSONPath expressions

Source: Internet
Author: User
Tags xpath

First, Jsonpath use the required package
<dependency>    <groupId>com.jayway.jsonpath</groupId>    <artifactId>json-path</artifactId>    <version>2.4.0</version></dependency>
Ii. Description of Use

1, Jsonpath is the application of XPath in JSON
2. JSONPath is the way in which XML documents are parsed by reference to XPath expressions, and the JSON data structure is usually anonymous and does not necessarily require a root element.
3. JSONPath uses an abstract name of $ to represent the outermost object
4. JSONPath allows the use of a wildcard * to denote the child element name and array index

Third, Jsonpath expression syntax

JSONPath expressions can use the. Symbol parsing JSON:
$.store.book[0].title
or use the [] symbol
$[' Store ' [' book '][0][' title ']

Iv. Test Examples

The contents of the JSON file are as follows:

{"Store": {"Book": [{"Category": "reference",  "author":  "Nigel Rees" ,  "title":  "sayings of the Century", 8.95}, {" category ": " author ": " Evelyn Waugh ",  "title":  "Sword of honour",  "price":  12.99,  "ISBN":  "0-553-21311-3"}],  " Bicycle ": {" color ": " Red ", " price ": 19.95}}            

First, read the JSON file, using the Commons.io fileutils readfiletostring method:

String path =System.getProperty("user.dir")+File.separator+"testdata"+File.separator+"test.json";String jsonString = FileUtils.readFileToString(new File(path),"utf-8");ReadContext context = JsonPath.parse(json);

Second, the output BOOK[1] author value. There are two ways of doing this:
Method One:

JsonPath.read(json,"$.store.book[1].author");或context.read("$.store.book[1].author");输出:Evelyn Waugh

Method Two:

JsonPath.read(json,"$[‘store‘][‘book‘][1][‘author‘]");context.read("$[‘store‘][‘book‘][1][‘author‘]");输出:Evelyn Waugh

Output BOOK[*] The Book of category = = ' reference '

List<Object> categorys = context.read("$.store.book[?(@.category == ‘reference‘)]");for(Object st: categorys){ System.out.println(st.toString());}输出: {category=reference, author=Nigel Rees, title=Sayings of the Century, price=8.95}

PRICE>10 book in output BOOK[*]

List<Object> prices = context.read("$.store.book[?(@.price>10)]");for(Object p:prices){ System.out.println(p.toString());}输出:{category=fiction, author=Evelyn Waugh, title=Sword of Honour, price=12.99, isbn=0-553-21311-3}

Bicycle[*] contains a color element in the bicycle

List<Object> color = context.read("$.store.bicycle[?(@.color)]");for(Object is :color){ System.out.println(is.toString());}输出://{color=red, price=19.95}

Outputs the value of all price in the JSON

list<object> pp = context.read ( "$ .. Price "); for (object p:p p) {system.out.println (P.tostring ());} Output: 8.95 12.99 19.95list<string> authors = Context.read (" $.) Store.book[*].author "); for (string str:authors) {System.out.< Span class= "hljs-built_in" >println (str);} Output: nigel rees evelyn waugh              
V. Comparison of methods for obtaining elements in XPATH and Jsonpath

[] The XPath expression always operates from the previous path to the array, and the index starts at 1.
Using the Josnpath [] operator to manipulate an object or an array, the index starts at 0.




Sunday
Links: https://www.jianshu.com/p/94ddacae5d55
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

Use of JSONPath expressions

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.