Gabbi 1.13.0 has been released with additional JSONPath features.
Gabbi is a tool for writing tests of HTTP APIs in a declarative style using a YAML-based format. It is primarily oriented towards JSON-based APIs, using JSONPath to validate and inspect responses.
If we have an API that returns a JSON response that looks like this:
{"pets": {
"cat": {
"sound": "meow",
},
"dog": {
"sound": "woof",
}
}
then a gabbi test for that API might look like this:
tests:
- name: get pets
GET: /pets
response_headers:
content-type: application/json
response_json_paths:
$.pets.cat.sound: meow
$.pets.dog.sound: woof
This form has always worked in gabbi. However when the JSON is structured like this:
{"pets": [
{"type": "cat", "sound": "meow"},
{"type": "dog", "sound": "woof"}
]}
where the contents of the pets
list is in an unpredictable order,
gabbi struggles: Until 1.13.0
gabbi could neither sort nor
filter in a JSONPath statement.
Now it can. These tests will now work, given the data above:
tests:
- name: get pets
GET: /pets
response_headers:
content-type: application/json
response_json_paths:
# sort by type
$.pets[/type][0].sound: meow
# sort by type, reversed
$.pets[\type][0].sound: woof
# all the sounds
$.pets[/type]..sound: ['meow', 'woof']
# filter by type = dog
$.pets[?type = "dog"].sound: woof
These features are necessary because JSON APIs love nested anonymous objects within a list, despite the fact that this makes it harder to extract a single piece of information.