This post illustrates 3 simple examples for testing a JSON API using Assertible. These examples assume you have an Assertible account with a valid web service and test. If you don't have a web service, just click here.
Assertible is free to use if you need an account.
1. Check basic JSON value using key
In this scenario, the goal is to extract a field from a JSON object and validate the expected value.
The JSON Object below is the Assertible API's HTTP error response for an unauthenticated request:
{
"code":"AuthenticationError",
"message":"Not logged in"
}
In the Assertible dashboard, navigate to your test, and click the Assertions configuration tab. In the Assertion type drop-down, select JSON path data.
The JSON Path Data assertion uses a specific syntax known as JSONPath to extract nested data withing a JSON document.
Configuring the assertion takes 3 inputs:
- JSON selector ->
.code
- Comparison ->
Equals
- Target value ->
AuthenticationError
When you have filled out the form, click Save assertion to finalize your results. Next, run the test (in some cases, the test may run automatically). If the test passes, the results will reflect the successful assertion.
length()
)
2. Minimum array length (using Another common use-case when creating tests for JSON documents is to check the length of an array. In many cases, you may want to check that an array is not empty, or has a certain minimum length.
Using the example JSON object below, I want to assert that the length
of the places
array is greater than three.
{
"places": ["one","two","three"]
}
In the Assertible dashboard, navigate to your test, and click the Assertions configuration tab. In the Assertion type drop-down, select JSON path data and set the form fields:
- JSON selector ->
.places.length()
- Comparison ->
GreatherThan
- Target value ->
3
The length()
function is a special convenience function that can be
appended to any object selector and works on JSON objects and arrays.
3. Validate JSON using JSON Schema Version 4
A standardized way to check the validity of JSON objects is to use JSON Schema. Assertible has a JSON Validation assertion with first-class support for JSON Schema Draft 4.
For example, if I have the following JSON object (an Assertible API error message):
{
"code":"AuthenticationError",
"message":"Not logged in"
}
A valid JSON Schema for the object would look like:
{
"type": "object",
"properties": {
"code": { "type":"string"},
"message": { "type":"string"}
}
}
To continuously validate that the schema validates against the HTTP response, navigate to your test, and click the Assertions configuration tab. In the Assertion type drop-down, select JSON Validation. Finally, select the JSON Schema Validation radio button and insert your JSON Schema into the input. The assertion will end up looking like this.
Finalize the assertion by clicking Save assertion button. Note that selecting the other JSON Validation radio button will simply make sure the JSON object is well-formed and parsable.
More test cases
One cool thing about Assertible is you can configure multiple assertions on a single test case. We frequently use JSON Schema validation in combination with JSON path data, to continuously test the Assertible API.
Get started testing your JSON API now!
Assertible is free to use. Contact us if you have any questions or
feedback!
More docs (who doesn't love docs?):
Is there an assertion or feature you'd like to see? Send us a message or reach out on Twitter and let's talk!
:: Christopher Reichert
Categories
The easiest way to test and
monitor your web services
Reduce bugs in web applications by using Assertible to create an automated QA pipeline that helps you catch failures & ship code faster.
Get started with GitHubSign up for freeRecent posts
Tips for importing and testing your API spec with Assertible 05/26/2020
New feature: Encrypted variables 10/30/2019
New feature: Smarter notifications 5/17/2019