Class JsonPath
{ "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(Object).get("store.book.category");
Get the first book category:
String category = with(Object).get("store.book[0].category");
Get the last book category:
String category = with(Object).get("store.book[-1].category");
Get all books with price between 5 and 15:
List<Map> books = with(Object).get("store.book.findAll { book -> book.price >= 5 && book.price <= 15 }");
The JsonPath implementation of rest-assured uses a Groovy shell to evaluate expressions so be careful when injecting
user input into the expression. For example avoid doing this:
String name = System.console().readLine();
List<Map> books = with(Object).get("store.book.findAll { book -> book.author == " + name + " }");
Instead use the param(java.lang.String, java.lang.Object) method like this:
String name = System.console().readLine();
List<Map> books = with(Object).param("name", name).get("store.book.findAll { book -> book.author == name }");
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionInstantiate a new JsonPath instance.JsonPath(InputStream stream) Instantiate a new JsonPath instance.Instantiate a new JsonPath instance.Instantiate a new JsonPath instance.Instantiate a new JsonPath instance. -
Method Summary
Modifier and TypeMethodDescriptionand()Syntactic sugar.static JsonPathInstantiate a new JsonPath instance.static JsonPathfrom(InputStream stream) Instantiate a new JsonPath instance.static JsonPathInstantiate a new JsonPath instance.static JsonPathInstantiate a new JsonPath instance.static JsonPathInstantiate a new JsonPath instance.<T> Tget()Get a Object graph with no named root element as a Java object.<T> TGet the result of an Object path expression as a boolean.booleangetBoolean(String path) Get the result of an Object path expression as a booleanbyteGet the result of an Object path expression as a byte.charGet the result of an Object path expression as a char.doubleGet the result of an Object path expression as a double.floatGet the result of an Object path expression as a float.intGet the result of an Object path expression as an int.<T> TgetJsonObject(String path) <T> List<T>Get the result of an Object path expression as a list.<T> List<T>Get the result of an Object path expression as a list.longGet the result of an Object path expression as a long.<K,V> Map<K, V> Get the result of an Object path expression as a map.<K,V> Map<K, V> Get the result of an Object path expression as a map.<T> TGet the result of a Object path expression as a java Object with generic type.<T> TGet the result of a Object path expression as a java Object.shortGet the result of an Object path expression as a short.Get the result of an Object path expression as a string.Get the result of an Object path expression as a UUID.static JsonPathInstantiate a new JsonPath instance.static JsonPathgiven(InputStream stream) Instantiate a new JsonPath instance.static JsonPathInstantiate a new JsonPath instance.static JsonPathInstantiate a new JsonPath instance.static JsonPathInstantiate a new JsonPath instance.Add a parameter for the expression.peek()Peeks into the JSON that JsonPath will parse by printing it to the console.prettify()Get the JSON as a prettified string.Peeks into the JSON that JsonPath will parse by printing it to the console in a prettified manner.Get and print the JSON as a prettified string.static voidreset()Resets static JsonPath configuration to default valuesDeprecated.setRootPath(String rootPath) Set the root path of the document so that you don't need to write the entire path.using(JsonPathConfig config) Configure JsonPath to with a specific JsonPathConfig.using(GsonObjectMapperFactory factory) Configure JsonPath to use a specific Gson object mapper factoryusing(Jackson1ObjectMapperFactory factory) Configure JsonPath to use a specific Jackson object mapper factoryusing(Jackson2ObjectMapperFactory factory) Configure JsonPath to use a specific Jackson 2 object mapper factorystatic JsonPathInstantiate a new JsonPath instance.static JsonPathwith(InputStream stream) Instantiate a new JsonPath instance.static JsonPathInstantiate a new JsonPath instance.static JsonPathInstantiate a new JsonPath instance.static JsonPathInstantiate a new JsonPath instance.
-
Field Details
-
config
-
-
Constructor Details
-
JsonPath
Instantiate a new JsonPath instance.- Parameters:
text- The text containing the Object document
-
JsonPath
Instantiate a new JsonPath instance.- Parameters:
url- The url containing the Object document
-
JsonPath
Instantiate a new JsonPath instance.- Parameters:
stream- The stream containing the Object document
-
JsonPath
Instantiate a new JsonPath instance.- Parameters:
file- The file containing the Object document
-
JsonPath
Instantiate a new JsonPath instance.- Parameters:
reader- The reader containing the Object document
-
-
Method Details
-
get
public <T> T get()Get a Object graph with no named root element as a Java object. This is just a short-cut forget("");orget("$");- Returns:
- The object matching the Object graph. This may be any primitive type, a List or a Map. A
ClassCastExceptionwill be thrown if the object cannot be casted to the expected type.
-
get
Get the result of an Object path expression as a boolean.- Parameters:
path- The Object path.- Returns:
- The object matching the Object path. This may be any primitive type, a List or a Map. A
ClassCastExceptionwill be thrown if the object cannot be casted to the expected type.
-
getBoolean
Get the result of an Object path expression as a boolean- Parameters:
path- The Object path.- Returns:
- The object matching the Object path. A
ClassCastExceptionwill be thrown if the object cannot be casted to the expected type.
-
getChar
Get the result of an Object path expression as a char.- Parameters:
path- The Object path.- Returns:
- The object matching the Object path. A
ClassCastExceptionwill be thrown if the object cannot be casted to the expected type.
-
getInt
Get the result of an Object path expression as an int.- Parameters:
path- The Object path.- Returns:
- The int matching the Object path. A
ClassCastExceptionwill be thrown if the object cannot be casted to the expected type.
-
getByte
Get the result of an Object path expression as a byte.- Parameters:
path- The Object path.- Returns:
- The object matching the Object path. A
ClassCastExceptionwill be thrown if the object cannot be casted to the expected type.
-
getShort
Get the result of an Object path expression as a short.- Parameters:
path- The Object path.- Returns:
- The object matching the Object path. A
ClassCastExceptionwill be thrown if the object cannot be casted to the expected type.
-
getFloat
Get the result of an Object path expression as a float.- Parameters:
path- The Object path.- Returns:
- The object matching the Object path. A
ClassCastExceptionwill be thrown if the object cannot be casted to the expected type.
-
getDouble
Get the result of an Object path expression as a double.- Parameters:
path- The Object path.- Returns:
- The object matching the Object path. A
ClassCastExceptionwill be thrown if the object cannot be casted to the expected type.
-
getLong
Get the result of an Object path expression as a long.- Parameters:
path- The Object path.- Returns:
- The object matching the Object path. A
ClassCastExceptionwill be thrown if the object cannot be casted to the expected type.
-
getString
Get the result of an Object path expression as a string.- Parameters:
path- The Object path.- Returns:
- The object matching the Object path. A
ClassCastExceptionwill be thrown if the object cannot be casted to the expected type.
-
getUUID
Get the result of an Object path expression as a UUID.- Parameters:
path- The Object path.- Returns:
- The object matching the Object path. A
ClassCastExceptionwill be thrown if the object cannot be casted to the expected type.
-
getList
Get the result of an Object path expression as a list.- Type Parameters:
T- The list type- Parameters:
path- The Object path.- Returns:
- The object matching the Object path. A
ClassCastExceptionwill be thrown if the object cannot be casted to the expected type.
-
getList
Get the result of an Object path expression as a list.- Type Parameters:
T- The type- Parameters:
path- The Object path.genericType- The generic list type- Returns:
- The object matching the Object path. A
ClassCastExceptionwill be thrown if the object cannot be casted to the expected type.
-
getMap
Get the result of an Object path expression as a map.- Type Parameters:
K- The type of the expected keyV- The type of the expected value- Parameters:
path- The Object path.- Returns:
- The map matching the Object path. A
ClassCastExceptionwill be thrown if the object cannot be casted to the expected type.
-
getMap
Get the result of an Object path expression as a map.- Type Parameters:
K- The type of the expected keyV- The type of the expected value- Parameters:
path- The Object path.keyType- The type of the expected keyvalueType- The type of the expected value- Returns:
- The map matching the Object path. A
ClassCastExceptionwill be thrown if the object cannot be casted to the expected type.
-
getObject
Get the result of a Object path expression as a java Object. E.g. given the following Object 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; } }ThenBook book = from(Object).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 mapobjectType- The class type of the expected object- Returns:
- The object
-
getObject
Get the result of a Object path expression as a java Object with generic type. E.g. given the following Object 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 you want to get a book as aMap<String, Object>: ThenMap<String, Object> book = from(Object).getObject("store.book[2]", new TypeRef<Map<String, Object>>() {});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 maptypeRef- The class type of the expected object- Returns:
- The object
-
param
Add a parameter for the expression. Example:String name = System.console().readLine(); List<Map> books = with(Object).param("name", name).get("store.book.findAll { book -> book.author == name }");- Parameters:
key- The name of the parameter. Just use this name in your expression as a variablevalue- The value of the parameter- Returns:
- New JsonPath instance with the parameter set
-
peek
Peeks into the JSON that JsonPath will parse by printing it to the console. You can continue working with JsonPath afterwards. This is mainly for debug purposes. If you want to return a prettified version of the content seeprettify(). If you want to return a prettified version of the content and also print it to the console useprettyPrint().Note that the content is not guaranteed to be looking exactly like the it does at the source. This is because once you peek the content has been downloaded and transformed into another data structure (used by JsonPath) and the JSON is rendered from this data structure.
- Returns:
- The same JsonPath instance
-
prettyPeek
Peeks into the JSON that JsonPath will parse by printing it to the console in a prettified manner. You can continue working with JsonPath afterwards. This is mainly for debug purposes. If you want to return a prettified version of the content seeprettify(). If you want to return a prettified version of the content and also print it to the console useprettyPrint().Note that the content is not guaranteed to be looking exactly like the it does at the source. This is because once you peek the content has been downloaded and transformed into another data structure (used by JsonPath) and the JSON is rendered from this data structure.
- Returns:
- The same JsonPath instance
-
prettify
Get the JSON as a prettified string.Note that the content is not guaranteed to be looking exactly like the it does at the source. This is because once you peek the content has been downloaded and transformed into another data structure (used by JsonPath) and the JSON is rendered from this data structure.
- Returns:
- The JSON as a prettified String.
-
prettyPrint
Get and print the JSON as a prettified string.Note that the content is not guaranteed to be looking exactly like the it does at the source. This is because once you peek the content has been downloaded and transformed into another data structure (used by JsonPath) and the JSON is rendered from this data structure.
- Returns:
- The JSON as a prettified String.
-
using
Configure JsonPath to use a specific Gson object mapper factory- Parameters:
factory- The gson object mapper factory instance- Returns:
- a new JsonPath instance
-
using
Configure JsonPath to use a specific Jackson object mapper factory- Parameters:
factory- The Jackson object mapper factory instance- Returns:
- a new JsonPath instance
-
using
Configure JsonPath to use a specific Jackson 2 object mapper factory- Parameters:
factory- The Jackson 2 object mapper factory instance- Returns:
- a new JsonPath instance
-
using
Configure JsonPath to with a specific JsonPathConfig.- Parameters:
config- The JsonPath config- Returns:
- a new JsonPath instance
-
and
Syntactic sugar.- Returns:
- The same JsonPath instance.
-
given
Instantiate a new JsonPath instance.- Parameters:
text- The text containing the Object document
-
given
Instantiate a new JsonPath instance.- Parameters:
stream- The stream containing the Object document
-
given
Instantiate a new JsonPath instance.- Parameters:
file- The file containing the Object document
-
given
Instantiate a new JsonPath instance.- Parameters:
reader- The reader containing the Object document
-
given
Instantiate a new JsonPath instance.- Parameters:
url- The URL containing the Object document
-
with
Instantiate a new JsonPath instance.- Parameters:
stream- The stream containing the Object document
-
with
Instantiate a new JsonPath instance.- Parameters:
text- The text containing the Object document
-
with
Instantiate a new JsonPath instance.- Parameters:
file- The file containing the Object document
-
with
Instantiate a new JsonPath instance.- Parameters:
reader- The reader containing the Object document
-
with
Instantiate a new JsonPath instance.- Parameters:
url- The URI containing the Object document
-
from
Instantiate a new JsonPath instance.- Parameters:
stream- The stream containing the Object document
-
from
Instantiate a new JsonPath instance.- Parameters:
text- The text containing the Object document
-
from
Instantiate a new JsonPath instance.- Parameters:
file- The file containing the Object document
-
from
Instantiate a new JsonPath instance.- Parameters:
reader- The reader containing the Object document
-
from
Instantiate a new JsonPath instance.- Parameters:
url- The URI containing the Object document
-
setRoot
Deprecated.UsesetRootPath(String)insteadSet the root path of the document so that you don't need to write the entire path. E.g.final JsonPath jsonPath = new JsonPath(Object).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.
-
setRootPath
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(Object).setRootPath("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
-
reset
public static void reset()Resets static JsonPath configuration to default values
-
setRootPath(String)instead