This site uses cookies. By continuing, your consent is assumed. Learn more

139.3fm shares

Validating json response object

opinion

Testing and validating JSON APIs is an important aspect of running a quality web service, but managing checks for large and diverse response bodies can be difficult.

I've been through the same...

API consumption is on the rise everywhere from businesses to personal projects, and they're constantly being changed to support new features, fix bugs, and change functionality. If you're tasked with testing an API, or you've hit breaking-change issues in a service you work on, you already realize the importance of having Validating json response object definition, a schema, that describes the data your service will accept and the data it will return. JSON Schema is a great tool for the job.

Properties within the schema are...

In this post I want to illustrate how you can continuously test and monitor your API responses, ensuring they adhere to a specified format, and get alerted when breaking changes occur. This is the definition from json-schema. It describes your existing data format with clear, human- and machine-readable documentation for complete structural validation, useful for automated testing and validating client-submitted data.

Naturally, you can recurse into each user and describe the format of its fields as well.

Read JSON Response Body using...

There are many tools and libraries available to create and work with JSON Schemas, making getting started easy. From there, automated monitoring and alerts can be set up for continuous testing. It's worth investigating other tools available, especially libraries in your programming language of choice. JSON Schema is a robust tool that has many application useful for all sorts of validation and documentation.

The structure of this JSON response has a few important things to point out, let's dissect it:.

Properties within the schema are...

It details the type and structure of the entire body, Validating json response object well as each individual field. To go over a few points:. It's a great tool for documenting your API responses and has a lot of benefits and use-cases that can be adapted. In addition to defining the shape and data type of the JSON, you can also use things like format to state a specific field should match a regex. Additionally, there is support for things like examples and descriptionswhich can be a great way to create a definition and "Validating json response object" at the same time.

The documentation on GitHub has a great overview of the specification. In this post, we're primarily concerned with making sure the actual API response matches this schema.

The next section will describe how to use Assertible to make this check. For more help on this, check out the quick start guide.

Let's walk through it:. You will see a form like this:. And there you have it! The beauty of the JSON Schema assertion is that the schema can be as simple or complex, as thorough or as sparse, as you need.

You may only want to check one or two fields, or you can check the entire document. With Assertible, it's easy to put this test on a Validating json response object for automated testing and set up failure alerts. All alert and notification functionality is available on Assertible's free plan. You can send failure messages to your email with hooks or to a team chat using the Slack integration.

Assertible is one way to run this test on your API, but of course there are many other tools for working with JSON Schema, especially as programming language libraries. In any case, this type of testing is valuable and I highly recommend integrating it with your QA and testing process.

It helps cover large Validating json response object of data quickly and removes the need to create custom checks for each API endpoint, response, and field. Let me know and I'll add it! The benefits of this are wide-ranging: There are a few other great blog posts I can across while researching this topic that are worth reading:. If you have any questions, comments, or feedback on the post, I'd love to hear from you!

Find us on Twitter or reach out any time and let's chat! The API returns a top level array Each array entry is an object a user. Each user has a firstNamelastNameand age key.

The firstName and lastName are strings, while the age is a number. To go over a few points: The top-level type key tells us the API returns an "array". The items describes the shape of each array entry each user.

Testing and validating JSON APIs...

The firstName and lastName fields are strings, while age is an integer. Let's walk through it: You will see a form like this: Automation and alerts With Assertible, it's easy to put this test on a schedule for automated Validating json response object and set up failure alerts.

Other tools Assertible is one way to run this test on your API, but of course there are many other tools for working with JSON Schema, especially as programming language libraries. Additional resources Validating json response object are a few other great blog posts I can across while researching this topic that are worth reading: Categories All Featured Engineering Changelog.

News feed