com.jayway.jsonpath
Class JsonPath

java.lang.Object
  extended by com.jayway.jsonpath.JsonPath

public class JsonPath
extends Object

User: kalle stenflo Date: 2/2/11 Time: 1:03 PM

JsonPath is to JSON what XPATH is to XML, a simple way to extract parts of a given document. JsonPath is available in many programming languages such as Javascript, Python and PHP.

JsonPath allows you to compile a json path string to use it many times or to compile and apply in one single on demand operation.

Given the Json document:

String json = "{ "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 } ], "bicycle": { "color": "red", "price": 19.95 } } }";

A JsonPath can be compiled and used as shown:

JsonPath path = JsonPath.compile("$.store.book[1]");
List<Object> books = path.read(json);

Or:

List<Object> authors = JsonPath.read(json, "$.store.book[*].author")

If the json path returns a single value (is definite):

String author = JsonPath.read(json, "$.store.book[1].author")


Method Summary
static JsonPath compile(String jsonPath)
          Compiles a JsonPath from the given string
<T> List<T>
read(Object json)
          Applies this json path to the provided object
static
<T> List<T>
read(Object json, String jsonPath)
          Creates a new JsonPath and applies it to the provided Json object
<T> List<T>
read(String json)
          Applies this json path to the provided object
static
<T> List<T>
read(String json, String jsonPath)
          Creates a new JsonPath and applies it to the provided Json string
static
<T> T
readOne(Object json, String jsonPath)
          Creates a new JsonPath and applies it to the provided Json object.
static
<T> T
readOne(String json, String jsonPath)
          Creates a new JsonPath and applies it to the provided Json object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

read

public <T> List<T> read(Object json)
Applies this json path to the provided object

Type Parameters:
T -
Parameters:
json - a json Object
Returns:
list of objects matched by the given path

read

public <T> List<T> read(String json)
             throws ParseException
Applies this json path to the provided object

Type Parameters:
T -
Parameters:
json - a json string
Returns:
list of objects matched by the given path
Throws:
ParseException

compile

public static JsonPath compile(String jsonPath)
Compiles a JsonPath from the given string

Parameters:
jsonPath - to compile
Returns:
compiled JsonPath

read

public static <T> List<T> read(String json,
                               String jsonPath)
                    throws ParseException
Creates a new JsonPath and applies it to the provided Json string

Type Parameters:
T -
Parameters:
json - a json string
jsonPath - the json path
Returns:
list of objects matched by the given path
Throws:
ParseException

read

public static <T> List<T> read(Object json,
                               String jsonPath)
Creates a new JsonPath and applies it to the provided Json object

Type Parameters:
T -
Parameters:
json - a json object
jsonPath - the json path
Returns:
list of objects matched by the given path

readOne

public static <T> T readOne(Object json,
                            String jsonPath)
Creates a new JsonPath and applies it to the provided Json object. Note this method will throw an exception if the provided path returns more than one object. This method can be used with paths that are not definite but a warning will be generated.

Type Parameters:
T -
Parameters:
json - a json object
jsonPath - the json path
Returns:
the object matched by the given path

readOne

public static <T> T readOne(String json,
                            String jsonPath)
                 throws ParseException
Creates a new JsonPath and applies it to the provided Json object. Note this method will throw an exception if the provided path returns more than one object. This method can be used with paths that are not definite but a warning will be generated.

Type Parameters:
T -
Parameters:
json - a json string
jsonPath - the json path
Returns:
the object matched by the given path
Throws:
ParseException


Copyright © 2011. All Rights Reserved.