com.jayway.restassured.path.json
Class JsonPath

java.lang.Object
  extended by com.jayway.restassured.path.json.JsonPath

public class JsonPath
extends Object

JsonPath is an alternative to using XPath for easily getting values from a JSON document. It follows the Groovy dot notation syntax when getting an object from the document. You can regard it as an alternative to XPath for XML. E.g. given the following JSON document:

 { "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": "J. R. R. Tolkien",
      "title": "The Lord of the Rings",
      "isbn": "0-395-19395-8",
      "price": 22.99
    }
  ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  }
 }
 
To get a list of all book categories:
 List<String> categories = with(JSON).get("store.book.category");
 
Get the first book category:
 String category = with(JSON).get("store.book[0].category");
 
Get the last book category:
 String category = with(JSON).get("store.book[-1].category");
 
Get all books with price between 5 and 15:
 List<Map> books = with(JSON).get("store.book.findAll { book -> book.price >= 5 && book.price <= 15 }");
 


Constructor Summary
JsonPath(File file)
          Instantiate a new JsonPath instance.
JsonPath(InputStream stream)
          Instantiate a new JsonPath instance.
JsonPath(Reader reader)
          Instantiate a new JsonPath instance.
JsonPath(String text)
          Instantiate a new JsonPath instance.
JsonPath(URL url)
          Instantiate a new JsonPath instance.
 
Method Summary
static JsonPath from(File file)
          Instantiate a new JsonPath instance.
static JsonPath from(InputStream stream)
          Instantiate a new JsonPath instance.
static JsonPath from(Reader reader)
          Instantiate a new JsonPath instance.
static JsonPath from(String text)
          Instantiate a new JsonPath instance.
static JsonPath from(URL url)
          Instantiate a new JsonPath instance.
<T> T
get()
          Get a JSON graph with no named root element as a Java object.
<T> T
get(String path)
          Get the result of an JSON path expression as a boolean.
 boolean getBoolean(String path)
          Get the result of an JSON path expression as a boolean
 byte getByte(String path)
          Get the result of an JSON path expression as a byte.
 char getChar(String path)
          Get the result of an JSON path expression as a char.
 double getDouble(String path)
          Get the result of an JSON path expression as a double.
 float getFloat(String path)
          Get the result of an JSON path expression as a float.
 int getInt(String path)
          Get the result of an JSON path expression as an int.
<T> T
getJsonObject(String path)
           
<T> List<T>
getList(String path)
          Get the result of an JSON path expression as a list.
<T> List<T>
getList(String path, Class<T> genericType)
          Get the result of an JSON path expression as a list.
 long getLong(String path)
          Get the result of an JSON path expression as a long.
<K,V> Map<K,V>
getMap(String path)
          Get the result of an JSON path expression as a map.
<K,V> Map<K,V>
getMap(String path, Class<K> keyType, Class<V> valueType)
          Get the result of an JSON path expression as a map.
<T> T
getObject(String path, Class<T> objectType)
          Get the result of a JSON path expression as a java Object.
 short getShort(String path)
          Get the result of an JSON path expression as a short.
 String getString(String path)
          Get the result of an JSON path expression as a string.
static JsonPath given(File file)
          Instantiate a new JsonPath instance.
static JsonPath given(InputStream stream)
          Instantiate a new JsonPath instance.
static JsonPath given(Reader reader)
          Instantiate a new JsonPath instance.
static JsonPath given(String text)
          Instantiate a new JsonPath instance.
static JsonPath given(URL url)
          Instantiate a new JsonPath instance.
 JsonPath setRoot(String rootPath)
          Set the root path of the document so that you don't need to write the entire path.
static JsonPath with(File file)
          Instantiate a new JsonPath instance.
static JsonPath with(InputStream stream)
          Instantiate a new JsonPath instance.
static JsonPath with(Reader reader)
          Instantiate a new JsonPath instance.
static JsonPath with(String text)
          Instantiate a new JsonPath instance.
static JsonPath with(URL url)
          Instantiate a new JsonPath instance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JsonPath

public JsonPath(String text)
Instantiate a new JsonPath instance.

Parameters:
text - The text containing the JSON document

JsonPath

public JsonPath(URL url)
Instantiate a new JsonPath instance.

Parameters:
url - The url containing the JSON document

JsonPath

public JsonPath(InputStream stream)
Instantiate a new JsonPath instance.

Parameters:
stream - The stream containing the JSON document

JsonPath

public JsonPath(File file)
Instantiate a new JsonPath instance.

Parameters:
file - The file containing the JSON document

JsonPath

public JsonPath(Reader reader)
Instantiate a new JsonPath instance.

Parameters:
reader - The reader containing the JSON document
Method Detail

get

public <T> T get()
Get a JSON graph with no named root element as a Java object. This is just a short-cut for
     get("");
 
or
     get("$");
 

Returns:
The object matching the JSON graph. This may be any primitive type, a List or a Map. A will be thrown if the object cannot be casted to the expected type.

get

public <T> T get(String path)
Get the result of an JSON path expression as a boolean.

Parameters:
path - The JSON path.
Returns:
The object matching the JSON path. This may be any primitive type, a List or a Map. A will be thrown if the object cannot be casted to the expected type.

getBoolean

public boolean getBoolean(String path)
Get the result of an JSON path expression as a boolean

Parameters:
path - The JSON path.
Returns:
The object matching the JSON path. A will be thrown if the object cannot be casted to the expected type.

getChar

public char getChar(String path)
Get the result of an JSON path expression as a char.

Parameters:
path - The JSON path.
Returns:
The object matching the JSON path. A will be thrown if the object cannot be casted to the expected type.

getInt

public int getInt(String path)
Get the result of an JSON path expression as an int.

Parameters:
path - The JSON path.
Returns:
The int matching the JSON path. A will be thrown if the object cannot be casted to the expected type.

getByte

public byte getByte(String path)
Get the result of an JSON path expression as a byte.

Parameters:
path - The JSON path.
Returns:
The object matching the JSON path. A will be thrown if the object cannot be casted to the expected type.

getShort

public short getShort(String path)
Get the result of an JSON path expression as a short.

Parameters:
path - The JSON path.
Returns:
The object matching the JSON path. A will be thrown if the object cannot be casted to the expected type.

getFloat

public float getFloat(String path)
Get the result of an JSON path expression as a float.

Parameters:
path - The JSON path.
Returns:
The object matching the JSON path. A will be thrown if the object cannot be casted to the expected type.

getDouble

public double getDouble(String path)
Get the result of an JSON path expression as a double.

Parameters:
path - The JSON path.
Returns:
The object matching the JSON path. A will be thrown if the object cannot be casted to the expected type.

getLong

public long getLong(String path)
Get the result of an JSON path expression as a long.

Parameters:
path - The JSON path.
Returns:
The object matching the JSON path. A will be thrown if the object cannot be casted to the expected type.

getString

public String getString(String path)
Get the result of an JSON path expression as a string.

Parameters:
path - The JSON path.
Returns:
The object matching the JSON path. A will be thrown if the object cannot be casted to the expected type.

getList

public <T> List<T> getList(String path)
Get the result of an JSON path expression as a list.

Type Parameters:
T - The list type
Parameters:
path - The JSON path.
Returns:
The object matching the JSON path. A will be thrown if the object cannot be casted to the expected type.

getList

public <T> List<T> getList(String path,
                           Class<T> genericType)
Get the result of an JSON path expression as a list.

Type Parameters:
T - The type
Parameters:
path - The JSON path.
genericType - The generic list type
Returns:
The object matching the JSON path. A will be thrown if the object cannot be casted to the expected type.

getMap

public <K,V> Map<K,V> getMap(String path)
Get the result of an JSON path expression as a map.

Type Parameters:
K - The type of the expected key
V - The type of the expected value
Parameters:
path - The JSON path.
Returns:
The map matching the JSON path. A will be thrown if the object cannot be casted to the expected type.

getMap

public <K,V> Map<K,V> getMap(String path,
                             Class<K> keyType,
                             Class<V> valueType)
Get the result of an JSON path expression as a map.

Type Parameters:
K - The type of the expected key
V - The type of the expected value
Parameters:
path - The JSON path.
keyType - The type of the expected key
valueType - The type of the expected value
Returns:
The map matching the JSON path. A will be thrown if the object cannot be casted to the expected type.

getObject

public <T> T getObject(String path,
                       Class<T> objectType)
Get the result of a JSON path expression as a java Object. E.g. given the following JSON document:
 { "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": "J. R. R. Tolkien",
      "title": "The Lord of the Rings",
      "isbn": "0-395-19395-8",
      "price": 22.99
    }
  ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  }
 }
 
And a Java object like this:
 public class Book {
      private String category;
      private String author;
      private String title;
      private String isbn;
      private float price;

      public String getCategory() {
         return category;
      }

     public void setCategory(String category) {
         this.category = category;
     }

    public String getAuthor() {
          return author;
     }

    public void setAuthor(String author) {
         this.author = author;
    }

    public String getTitle() {
         return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getIsbn() {
             return isbn;
    }

    public void setIsbn(String isbn) {
          this.isbn = isbn;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
             this.price = price;
   }
 }
 
Then
 Book book = from(JSON).getObject("store.book[2]", Book.class);
 
maps the second book to a Book instance.

Type Parameters:
T - The type of the expected object
Parameters:
path - The path to the object to map
objectType - The class type of the expected object
Returns:
The object

given

public static JsonPath given(String text)
Instantiate a new JsonPath instance.

Parameters:
text - The text containing the JSON document

given

public static JsonPath given(InputStream stream)
Instantiate a new JsonPath instance.

Parameters:
stream - The stream containing the JSON document

given

public static JsonPath given(File file)
Instantiate a new JsonPath instance.

Parameters:
file - The file containing the JSON document

given

public static JsonPath given(Reader reader)
Instantiate a new JsonPath instance.

Parameters:
reader - The reader containing the JSON document

given

public static JsonPath given(URL url)
Instantiate a new JsonPath instance.

Parameters:
url - The URL containing the JSON document

with

public static JsonPath with(InputStream stream)
Instantiate a new JsonPath instance.

Parameters:
stream - The stream containing the JSON document

with

public static JsonPath with(String text)
Instantiate a new JsonPath instance.

Parameters:
text - The text containing the JSON document

with

public static JsonPath with(File file)
Instantiate a new JsonPath instance.

Parameters:
file - The file containing the JSON document

with

public static JsonPath with(Reader reader)
Instantiate a new JsonPath instance.

Parameters:
reader - The reader containing the JSON document

with

public static JsonPath with(URL url)
Instantiate a new JsonPath instance.

Parameters:
url - The URI containing the JSON document

from

public static JsonPath from(InputStream stream)
Instantiate a new JsonPath instance.

Parameters:
stream - The stream containing the JSON document

from

public static JsonPath from(String text)
Instantiate a new JsonPath instance.

Parameters:
text - The text containing the JSON document

from

public static JsonPath from(File file)
Instantiate a new JsonPath instance.

Parameters:
file - The file containing the JSON document

from

public static JsonPath from(Reader reader)
Instantiate a new JsonPath instance.

Parameters:
reader - The reader containing the JSON document

from

public static JsonPath from(URL url)
Instantiate a new JsonPath instance.

Parameters:
url - The URI containing the JSON document

setRoot

public JsonPath setRoot(String rootPath)
Set the root path of the document so that you don't need to write the entire path. E.g.
 final JsonPath jsonPath = new JsonPath(JSON).setRoot("store.book");
 assertThat(jsonPath.getInt("size()"), equalTo(4));
 assertThat(jsonPath.getList("author", String.class), hasItem("J. R. R. Tolkien"));
 

Parameters:
rootPath - The root path to use.

getJsonObject

public <T> T getJsonObject(String path)


Copyright © 2010-2011. All Rights Reserved.