How to search / find in json with java

I have a line below JSON from below. I want to find / find criteria in a JSON string.

1). Find the number of keys present. 2). To get the values ​​of a given key (if we have an array)

{ "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 }, { "category": "fiction", "author": "JRR Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 } ], "bicycle": { "color": "red", "price": 19.95 } }, "expensive": 10 } 

I am looking for a solution similar to Groovy GPath Syntax

  • store.book is the size of this array.
  • store.book [*]. category - how can there be a key present in the array.
  • store.bicycle - if he found that he should return the true value
+5
source share
3 answers

You can also use the JsonPath project provided by REST Assured . This JsonPath project uses Groovy GPath expressions. In Maven, you can rely on this as follows:

 <dependency> <groupId>com.jayway.restassured</groupId> <artifactId>json-path</artifactId> <version>2.4.0</version> </dependency> 

<strong> Examples:

To get a list of all book categories:

 List<String> categories = JsonPath.from(json).get("store.book.category"); 

Get the first category of books:

 String category = JsonPath.from(json).get("store.book[0].category"); 

Get the latest category of books:

 String category = JsonPath.from(json).get("store.book[-1].category"); 

Get all books priced from 5 to 15:

 List<Map> books = JsonPath.from(json).get("store.book.findAll { book -> book.price >= 5 && book.price <= 15 }"); 

GPath is very efficient, and you can use higher order functions and all Groovy data structures in your path expressions.

+8
source

I would try the following library

https://github.com/jayway/JsonPath

http://jsonpath.herokuapp.com/?path=%24.store.book [ *]

gives you all the books

0
source

First of all, you need to add a dependency

 <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20090211</version> </dependency> 

Then you can use this code:

 import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.json.XML; public class Main { public static int PRETTY_PRINT_INDENT_FACTOR = 4; public static String TEST_XML_STRING = "<Result><ResultParameters><ResultParameter><Key>TransEndDate</Key><Value>20170731</Value></ResultParameter><ResultParameter><Key>Failed Reason</Key><Value>Operator 03058361178\03058365278 has no permission to perform the Check Balance service.</Value></ResultParameter><ResultParameter><Key>TransEndTime</Key><Value>123611</Value></ResultParameter></ResultParameters><ResultCode>2000</ResultCode><ResultType>0</ResultType><OriginatorConversationID>S_X20130129210125</OriginatorConversationID><ResultDesc>Initiator authentication error.</ResultDesc><TransactionID>000749213589</TransactionID><ConversationID>AG_20170731_00004c28e51f1f822e9e</ConversationID></Result>"; public static void main(String[] args) { try { JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING); String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR); System.out.println(jsonPrettyPrintString); System.out.println("Element="+xmlJSONObj.getJSONObject("Result").getInt("ResultCode")); } catch (JSONException je) { System.out.println(je.toString()); } } } // in it i am searching an element which is present in a json object 
0
source

Source: https://habr.com/ru/post/1215123/


All Articles