Checking properties of an array of objects#
Given the sample data set below, we want to check if the price
of each book is greater than 10
.
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95,
"tags": ["book", "Rees", "reference", "Sayings"]
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99,
"tags": ["book", "Waugh", "fiction", "Sword"]
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99,
"tags": ["book", "Melville", "fiction", "Moby"]
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99,
"tags": []
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
Steps:
Define a
ref
that returns an array of the objects you are interested in. In this case we want to check theprice
of eachbook
, so we can use$.store.book[*]
asref
.Note
You can use the jsonPath online evaluator to check your jsonPath syntax.
Define a
condition
that checks theprice
of eachbook
. In this case we can useall(ref, "$.price > 10")
ascondition
.Note
There are also other operators like
any
,none
andone
that you can use.
Your config file then looks like this:
checks:
- name: book_prices_greater_than_10
ref: $.store.book[*]
condition: all(ref, "$.price > 10")
For the example data set above, the result of this check would be: RED
.