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(JsonProvider provider, String jsonPath)
           
static JsonPath compile(String jsonPath)
          Compiles a JsonPath from the given string
 String getPath()
           
 boolean isPathDefinite()
          Checks if a path points to a single item or if it potentially returns multiple items

a path is considered not definite if it contains a scan fragment ".." or an array position fragment that is not based on a single index

definite path examples are:

$store.book $store.book[1].title

not definite path examples are:

$..book $.store.book[1,2] $.store.book[?(@.category = 'fiction')]

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

Method Detail

getPath

public String getPath()

isPathDefinite

public boolean isPathDefinite()
Checks if a path points to a single item or if it potentially returns multiple items

a path is considered not definite if it contains a scan fragment ".." or an array position fragment that is not based on a single index

definite path examples are:

$store.book $store.book[1].title

not definite path examples are:

$..book $.store.book[1,2] $.store.book[?(@.category = 'fiction')]

Returns:
true if path is definite (points to single item)

read

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

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

read

public <T> T read(String json)
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

compile

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

Parameters:
jsonPath - to compile
Returns:
compiled JsonPath

compile

public static JsonPath compile(JsonProvider provider,
                               String jsonPath)

read

public static <T> T read(String json,
                         String jsonPath)
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

read

public static <T> 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


Copyright © 2011-2012. All Rights Reserved.