public class JsonPath extends Object
{ "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 }");
Modifier and Type | Field and Description |
---|---|
static JsonPathConfig |
config |
Constructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
JsonPath |
and()
Syntactic sugar.
|
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 Object graph with no named root element as a Java object.
|
<T> T |
get(String path)
Get the result of an Object path expression as a boolean.
|
boolean |
getBoolean(String path)
Get the result of an Object path expression as a boolean
|
byte |
getByte(String path)
Get the result of an Object path expression as a byte.
|
char |
getChar(String path)
Get the result of an Object path expression as a char.
|
double |
getDouble(String path)
Get the result of an Object path expression as a double.
|
float |
getFloat(String path)
Get the result of an Object path expression as a float.
|
int |
getInt(String path)
Get the result of an Object path expression as an int.
|
<T> T |
getJsonObject(String path) |
<T> List<T> |
getList(String path)
Get the result of an Object path expression as a list.
|
<T> List<T> |
getList(String path,
Class<T> genericType)
Get the result of an Object path expression as a list.
|
long |
getLong(String path)
Get the result of an Object path expression as a long.
|
<K,V> Map<K,V> |
getMap(String path)
Get the result of an Object path expression as a map.
|
<K,V> Map<K,V> |
getMap(String path,
Class<K> keyType,
Class<V> valueType)
Get the result of an Object path expression as a map.
|
<T> T |
getObject(String path,
Class<T> objectType)
Get the result of a Object path expression as a java Object.
|
<T> T |
getObject(String path,
io.restassured.common.mapper.TypeRef<T> typeRef)
Get the result of a Object path expression as a java Object with generic type.
|
short |
getShort(String path)
Get the result of an Object path expression as a short.
|
String |
getString(String path)
Get the result of an Object path expression as a string.
|
UUID |
getUUID(String path)
Get the result of an Object path expression as a UUID.
|
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 |
param(String key,
Object value)
Add a parameter for the expression.
|
JsonPath |
peek()
Peeks into the JSON that JsonPath will parse by printing it to the console.
|
String |
prettify()
Get the JSON as a prettified string.
|
JsonPath |
prettyPeek()
Peeks into the JSON that JsonPath will parse by printing it to the console in a prettified manner.
|
String |
prettyPrint()
Get and print the JSON as a prettified string.
|
static void |
reset()
Resets static JsonPath configuration to default values
|
JsonPath |
setRoot(String rootPath)
Deprecated.
Use
setRootPath(String) instead |
JsonPath |
setRootPath(String rootPath)
Set the root path of the document so that you don't need to write the entire path.
|
JsonPath |
using(GsonObjectMapperFactory factory)
Configure JsonPath to use a specific Gson object mapper factory
|
JsonPath |
using(Jackson1ObjectMapperFactory factory)
Configure JsonPath to use a specific Jackson object mapper factory
|
JsonPath |
using(Jackson2ObjectMapperFactory factory)
Configure JsonPath to use a specific Jackson 2 object mapper factory
|
JsonPath |
using(JsonPathConfig config)
Configure JsonPath to with a specific JsonPathConfig.
|
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.
|
public static JsonPathConfig config
public JsonPath(String text)
text
- The text containing the Object documentpublic JsonPath(URL url)
url
- The url containing the Object documentpublic JsonPath(InputStream stream)
stream
- The stream containing the Object documentpublic JsonPath(File file)
file
- The file containing the Object documentpublic JsonPath(Reader reader)
reader
- The reader containing the Object documentpublic <T> T get()
get("");or
get("$");
ClassCastException
will be thrown if the object
cannot be casted to the expected type.public <T> T get(String path)
path
- The Object path.ClassCastException
will be thrown if the object
cannot be casted to the expected type.public boolean getBoolean(String path)
path
- The Object path.ClassCastException
will be thrown if the object
cannot be casted to the expected type.public char getChar(String path)
path
- The Object path.ClassCastException
will be thrown if the object
cannot be casted to the expected type.public int getInt(String path)
path
- The Object path.ClassCastException
will be thrown if the object
cannot be casted to the expected type.public byte getByte(String path)
path
- The Object path.ClassCastException
will be thrown if the object
cannot be casted to the expected type.public short getShort(String path)
path
- The Object path.ClassCastException
will be thrown if the object
cannot be casted to the expected type.public float getFloat(String path)
path
- The Object path.ClassCastException
will be thrown if the object
cannot be casted to the expected type.public double getDouble(String path)
path
- The Object path.ClassCastException
will be thrown if the object
cannot be casted to the expected type.public long getLong(String path)
path
- The Object path.ClassCastException
will be thrown if the object
cannot be casted to the expected type.public String getString(String path)
path
- The Object path.ClassCastException
will be thrown if the object
cannot be casted to the expected type.public UUID getUUID(String path)
path
- The Object path.ClassCastException
will be thrown if the object
cannot be casted to the expected type.public <T> List<T> getList(String path)
T
- The list typepath
- The Object path.ClassCastException
will be thrown if the object
cannot be casted to the expected type.public <T> List<T> getList(String path, Class<T> genericType)
T
- The typepath
- The Object path.genericType
- The generic list typeClassCastException
will be thrown if the object
cannot be casted to the expected type.public <K,V> Map<K,V> getMap(String path)
K
- The type of the expected keyV
- The type of the expected valuepath
- The Object path.ClassCastException
will be thrown if the object
cannot be casted to the expected type.public <K,V> Map<K,V> getMap(String path, Class<K> keyType, Class<V> valueType)
K
- The type of the expected keyV
- The type of the expected valuepath
- The Object path.keyType
- The type of the expected keyvalueType
- The type of the expected valueClassCastException
will be thrown if the object
cannot be casted to the expected type.public <T> T getObject(String path, Class<T> objectType)
{ "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(Object).getObject("store.book[2]", Book.class);maps the second book to a Book instance.
T
- The type of the expected objectpath
- The path to the object to mapobjectType
- The class type of the expected objectpublic <T> T getObject(String path, io.restassured.common.mapper.TypeRef<T> typeRef)
{ "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 a
Map<String, Object>
:
Then
Map<String, Object> book = from(Object).getObject("store.book[2]", new TypeRef<Map<String, Object>>() {});maps the second book to a Book instance.
T
- The type of the expected objectpath
- The path to the object to maptypeRef
- The class type of the expected objectpublic JsonPath param(String key, Object value)
String name = System.console().readLine(); List<Map> books = with(Object).param("name", name).get("store.book.findAll { book -> book.author == name }");
key
- The name of the parameter. Just use this name in your expression as a variablevalue
- The value of the parameterpublic JsonPath peek()
prettify()
. If you want to return a prettified version of the content and also print it to the console use prettyPrint()
.
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.
public JsonPath prettyPeek()
prettify()
. If you want to return a prettified version of the content and also print it to the console use prettyPrint()
.
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.
public String prettify()
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.
public String prettyPrint()
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.
public JsonPath using(GsonObjectMapperFactory factory)
factory
- The gson object mapper factory instancepublic JsonPath using(Jackson1ObjectMapperFactory factory)
factory
- The Jackson object mapper factory instancepublic JsonPath using(Jackson2ObjectMapperFactory factory)
factory
- The Jackson 2 object mapper factory instancepublic JsonPath using(JsonPathConfig config)
config
- The JsonPath configpublic JsonPath and()
public static JsonPath given(String text)
text
- The text containing the Object documentpublic static JsonPath given(InputStream stream)
stream
- The stream containing the Object documentpublic static JsonPath given(File file)
file
- The file containing the Object documentpublic static JsonPath given(Reader reader)
reader
- The reader containing the Object documentpublic static JsonPath given(URL url)
url
- The URL containing the Object documentpublic static JsonPath with(InputStream stream)
stream
- The stream containing the Object documentpublic static JsonPath with(String text)
text
- The text containing the Object documentpublic static JsonPath with(File file)
file
- The file containing the Object documentpublic static JsonPath with(Reader reader)
reader
- The reader containing the Object documentpublic static JsonPath with(URL url)
url
- The URI containing the Object documentpublic static JsonPath from(InputStream stream)
stream
- The stream containing the Object documentpublic static JsonPath from(String text)
text
- The text containing the Object documentpublic static JsonPath from(File file)
file
- The file containing the Object documentpublic static JsonPath from(Reader reader)
reader
- The reader containing the Object documentpublic static JsonPath from(URL url)
url
- The URI containing the Object document@Deprecated public JsonPath setRoot(String rootPath)
setRootPath(String)
insteadfinal 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"));
rootPath
- The root path to use.public JsonPath setRootPath(String rootPath)
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"));
rootPath
- The root path to use.public <T> T getJsonObject(String path)
public static void reset()
Copyright © 2010–2023. All rights reserved.