Showing posts with label JSONPath. Show all posts
Showing posts with label JSONPath. Show all posts

Saturday, 7 September 2019

JsonPath Syntax


Below table summarises the syntax that we should follow while extracting the data from json document.

JSONPath
Description
$
Represents root element
@
Represent current object
. or []
Child Operator. Used to access child document.
..
Recursive descent
*
All objects
[]
Array Operator
[,]
Union operator in XPath results in a combination of node sets. JSONPath allows alternate names or array indices as a set.
[start:end:step]
Array Slice Operator
?()
Applies a filter or expression
()
Script Expression, using the underlying script engine.

I am going to explain each JsonPath syntax in my upcoming posts.


Previous                                                    Next                                                    Home

JsonPath: Parse the document before querying


In my previous post, I explained how to extract the information from a json document using 'JsonPath.read()' method. If you want to extract the data once this is OK. In case you need to extract other parts of the data as well this is not the way to go since the document will be parsed every time you call JsonPath.read(...). To avoid the problem you can parse the json first.

Example
Object jsonDocument = Configuration.defaultConfiguration().jsonProvider().parse(json);

String firstOrganization = JsonPath.read(jsonDocument, "$.org[0].name");
String secondOrganization = JsonPath.read(jsonDocument, "$.org[1].name");
String firstName = JsonPath.read(jsonDocument, "$.empName.firstName");
String lastName = JsonPath.read(jsonDocument, "$.empName.lastName");
double salary = JsonPath.read(jsonDocument, "$.salary");

Input
{
 "org": [{
  "name": "Honeywell",
  "yrsOfExperience": 2.2
 }, {
  "name": "IBM",
  "yrsOfExperience": 1.8
 }],

 "empName": {
  "firstName": "Krishna",
  "lastName": "Rama"
 },

 "salary": 80000.0
}


App.java
package com.sample.app;

import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;

public class App {

 public static void main(String[] args) {

  String json = "{\n" + 
    " \"org\": [{\n" + 
    "  \"name\": \"Honeywell\",\n" + 
    "  \"yrsOfExperience\": 2.2\n" + 
    " }, {\n" + 
    "  \"name\": \"IBM\",\n" + 
    "  \"yrsOfExperience\": 1.8\n" + 
    " }],\n" + 
    "\n" + 
    " \"empName\": {\n" + 
    "  \"firstName\": \"Krishna\",\n" + 
    "  \"lastName\": \"Rama\"\n" + 
    " },\n" + 
    "\n" + 
    " \"salary\": 80000.0\n" + 
    "}";

  Object jsonDocument = Configuration.defaultConfiguration().jsonProvider().parse(json);
  
  String firstOrganization = JsonPath.read(jsonDocument, "$.org[0].name");
  String secondOrganization = JsonPath.read(jsonDocument, "$.org[1].name");
  String firstName = JsonPath.read(jsonDocument, "$.empName.firstName");
  String lastName = JsonPath.read(jsonDocument, "$.empName.lastName");
  double salary = JsonPath.read(jsonDocument, "$.salary");

  System.out.println("firstOrganization : " + firstOrganization);
  System.out.println("secondOrganization : " + secondOrganization);
  System.out.println("firstName : " + firstName);
  System.out.println("lastName : " + lastName);
  System.out.println("salary : " + salary);


 }

}

Output
firstOrganization : Honeywell
secondOrganization : IBM
firstName : Krishna
lastName : Rama
salary : 80000.0



Previous                                                    Next                                                    Home

JsonPath: Hello World Application


Let's extract organization names, employee firstName, lastName and salary from below json document.
{
 "org": [{
  "name": "Honeywell",
  "yrsOfExperience": 2.2
 }, {
  "name": "IBM",
  "yrsOfExperience": 1.8
 }],

 "empName": {
  "firstName": "Krishna",
  "lastName": "Rama"
 },

 "salary": 80000.0
}

"$.empName.firstName": Extract firstName from the document.
"$.empName.lastName": Extract lastNam from the document.
"$.org[0].name": Extract first organisation name
"$.org[1].name": Extract second organisation name

"$" represents root element.

Example
String firstOrganization = JsonPath.read(json, "$.org[0].name");
String secondOrganization = JsonPath.read(json, "$.org[1].name");
String firstName = JsonPath.read(json, "$.empName.firstName");
String lastName = JsonPath.read(json, "$.empName.lastName");
double salary = JsonPath.read(json, "$.salary");

App.java
package com.sample.app;

import com.jayway.jsonpath.JsonPath;

public class App {

 public static void main(String[] args) {

  String json = "{\n" + 
    " \"org\": [{\n" + 
    "  \"name\": \"Honeywell\",\n" + 
    "  \"yrsOfExperience\": 2.2\n" + 
    " }, {\n" + 
    "  \"name\": \"IBM\",\n" + 
    "  \"yrsOfExperience\": 1.8\n" + 
    " }],\n" + 
    "\n" + 
    " \"empName\": {\n" + 
    "  \"firstName\": \"Krishna\",\n" + 
    "  \"lastName\": \"Rama\"\n" + 
    " },\n" + 
    "\n" + 
    " \"salary\": 80000.0\n" + 
    "}";

  String firstOrganization = JsonPath.read(json, "$.org[0].name");
  String secondOrganization = JsonPath.read(json, "$.org[1].name");
  String firstName = JsonPath.read(json, "$.empName.firstName");
  String lastName = JsonPath.read(json, "$.empName.lastName");
  double salary = JsonPath.read(json, "$.salary");

  System.out.println("firstOrganization : " + firstOrganization);
  System.out.println("secondOrganization : " + secondOrganization);
  System.out.println("firstName : " + firstName);
  System.out.println("lastName : " + lastName);
  System.out.println("salary : " + salary);


 }

}


Output
firstOrganization : Honeywell
secondOrganization : IBM
firstName : Krishna
lastName : Rama
salary : 80000.0


Previous                                                    Next                                                    Home

Introduction to jsonPath


jsonPath is used to analyse, transform and selectively extract data out of json documents.

I am going to use below maven dependency.

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


Reference

Previous                                                    Next                                                    Home