Assertible's JSON path assertion syntax allows you to get capture valuesfrom JSON response bodies using a simple and familiar syntax. Currently,the syntax allows you to traverse keys and array indexes; advanced syntaxlike array slicing and javascript expressions are not supported.
If you have a use-case that's not yet supported by thissyntax, contact us and let us know!
Sections
- JSON Path examples
- JSON Path wildcard syntax
- Interactive JSON Path tester
JSON Path examples
key selection example
{ "id": 1, "firstName": "Chris", "foo": { "bar": "baz" } }
selector:
.firstName
result:
"Chris"
selector:
.foo.bar
result:
"baz"
key selection w/ dot in name
{ "name.with": 1 }
selector:
['name.with']
result:
1
key selection w/ space in name
See AlsoCreating a Custom Mapping FileJSON PointerFortux | JSONPath StandardWriting JSONPath Expressions - Hevo Data{ "name with": 1 }
selector:
['name with']
result:
1
array index
[ { "id": 1, "firstName": "Chris", } ]
selector:
[0].id
result:
1
array length
{ "users": [1,2,3,4,5] }
selector:
.users.length()
result:
5
object "length"
{ "steps": {"step1": 1, "step2": 2} }
selector:
.steps.length()
result:
2
string "length"
{ "email": "my@email.com" }
selector:
.email.length()
result:
12
The JSON path .length()
function can only be used at the end of aJSON path expression. For example, .foo[(@.length)]
and.foo[(.length())]
are not supported. .length()
can only be used onJSON arrays and objects.
JSON Path wildcard syntax
Assertible's JSON Path syntax has limited support for wildcard syntax onJSON arrays and objects.
IMPORTANT Assertible's JSON Path syntaxsupports wildcards in the path. Only single node selections are valid using purejson path. If you need more advanced scripting capabilities, use the.jq()
function.
array wildcard
{ "steps": [ { "step_num": 1, "description": "Description" }, { "step_num": 2, "description": "Description", "uncommon_field": "some data" } ] }
selector:
.steps[].uncommon_field
result:
"some data"
selector:
.steps[].step_num
result: Invalid selection - selecting multiple nodes is not supported.
object wildcard
{ "foo": { "id": 1, "data": "baz" }, "bar": { "id": 1, "data": "quux", "description": "hello world" }, "steps": [ { "step_num": 1, "description": "Description" }, { "step_num": 2, "description": "Description", "uncommon_field": "some data" } ] }
selector:
[*].description
result:
"hello world"
selector:
[].description
result:
"hello world"
selector:
[].data
result: Invalid selection - selecting multiple nodes is not supported.
Use the forms below to interactively test JSON Path queries against a JSON body. Enter a JSON object on the left, and enter a JSON Path query on the right. Check out the JSON Path examples above and if you have any questions you can contact us any time.
JSON body
JSON Path query
Result
"baz"