com.jayway.jsonpath
Class JsonPath

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

public class JsonPath
extends Object

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")

Author:
Kalle Stenflo

Constructor Summary
JsonPath(String jsonPath, Filter[] filters)
           
 
Method Summary
static JsonPath compile(String jsonPath, Filter... filters)
          Compiles a JsonPath
 JsonPath copy()
           
 String getPath()
          Returns the string representation of this JsonPath
 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(File jsonFile)
          Applies this JsonPath to the provided json file
static
<T> T
read(File jsonFile, String jsonPath, Filter... filters)
          Creates a new JsonPath and applies it to the provided Json object
<T> T
read(InputStream jsonInputStream)
          Applies this JsonPath to the provided json input stream
static
<T> T
read(InputStream jsonInputStream, String jsonPath, Filter... filters)
          Creates a new JsonPath and applies it to the provided Json object
<T> T
read(Object jsonObject)
          Applies this JsonPath to the provided json document.
static
<T> T
read(Object json, String jsonPath, Filter... filters)
          Creates a new JsonPath and applies it to the provided Json object
<T> T
read(String json)
          Applies this JsonPath to the provided json string
static
<T> T
read(String json, String jsonPath, Filter... filters)
          Creates a new JsonPath and applies it to the provided Json string
<T> T
read(URL jsonURL)
          Applies this JsonPath to the provided json URL
static
<T> T
read(URL jsonURL, String jsonPath, Filter... filters)
          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
 

Constructor Detail

JsonPath

public JsonPath(String jsonPath,
                Filter[] filters)
Method Detail

copy

public JsonPath copy()

getPath

public String getPath()
Returns the string representation of this JsonPath

Returns:
path as String

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 jsonObject)
Applies this JsonPath to the provided json document. Note that the document must either a List or a Map

Type Parameters:
T - expected return type
Parameters:
jsonObject - a container Object (List or Map)
Returns:
list of objects matched by the given path

read

public <T> T read(String json)
Applies this JsonPath to the provided json string

Type Parameters:
T - expected return type
Parameters:
json - a json string
Returns:
list of objects matched by the given path

read

public <T> T read(URL jsonURL)
       throws IOException
Applies this JsonPath to the provided json URL

Type Parameters:
T - expected return type
Parameters:
jsonURL - url to read from
Returns:
list of objects matched by the given path
Throws:
IOException

read

public <T> T read(File jsonFile)
       throws IOException
Applies this JsonPath to the provided json file

Type Parameters:
T - expected return type
Parameters:
jsonFile - file to read from
Returns:
list of objects matched by the given path
Throws:
IOException

read

public <T> T read(InputStream jsonInputStream)
       throws IOException
Applies this JsonPath to the provided json input stream

Type Parameters:
T - expected return type
Parameters:
jsonInputStream - input stream to read from
Returns:
list of objects matched by the given path
Throws:
IOException

compile

public static JsonPath compile(String jsonPath,
                               Filter... filters)
Compiles a JsonPath

Parameters:
jsonPath - to compile
filters - filters to be applied to the filter place holders [?] in the path
Returns:
compiled JsonPath

read

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

Type Parameters:
T - expected return type
Parameters:
json - a json string
jsonPath - the json path
filters - filters to be applied to the filter place holders [?] in the path
Returns:
list of objects matched by the given path

read

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

Type Parameters:
T - expected return type
Parameters:
json - a json object
jsonPath - the json path
filters - filters to be applied to the filter place holders [?] in the path
Returns:
list of objects matched by the given path

read

public static <T> T read(URL jsonURL,
                         String jsonPath,
                         Filter... filters)
              throws IOException
Creates a new JsonPath and applies it to the provided Json object

Type Parameters:
T - expected return type
Parameters:
jsonURL - url pointing to json doc
jsonPath - the json path
filters - filters to be applied to the filter place holders [?] in the path
Returns:
list of objects matched by the given path
Throws:
IOException

read

public static <T> T read(File jsonFile,
                         String jsonPath,
                         Filter... filters)
              throws IOException
Creates a new JsonPath and applies it to the provided Json object

Type Parameters:
T - expected return type
Parameters:
jsonFile - json file
jsonPath - the json path
filters - filters to be applied to the filter place holders [?] in the path
Returns:
list of objects matched by the given path
Throws:
IOException

read

public static <T> T read(InputStream jsonInputStream,
                         String jsonPath,
                         Filter... filters)
              throws IOException
Creates a new JsonPath and applies it to the provided Json object

Type Parameters:
T - expected return type
Parameters:
jsonInputStream - json input stream
jsonPath - the json path
filters - filters to be applied to the filter place holders [?] in the path
Returns:
list of objects matched by the given path
Throws:
IOException


Copyright © 2011-2012. All Rights Reserved.