How do I parse and validate a JSON response?

If you’re load testing a JSON web service, or something similar, it’s important to validate that the responses coming back are of a valid JSON format and are structured correctly. Furthermore, it’s often important to look for certain pieces of data in the response to make sure it’s correct.

The easy way to do this in Loadster is with the “Validate with JavaScript” feature. Expand the Add… menu on the step you want to validate, and select Validate with JavaScript from the menu.

Adding a JavaScript validator

This type of validator lets you write a custom JavaScript function to evaluate the response.

Parsing a JSON array and looking at the first item

To parse the JSON into a JavaScript object, use the standard JSON.parse function.

The important thing about validation functions is that they’re expected to return a boolean value: true if the response is valid, and false if it’s invalid. In the above example, we’re only considering the response valid if the response is a JSON array with at least 1 item, and the first item has a firstName of “Alice”.

You can read more about validation in Loadster, and other types of validators, in Validation Rules.

JSON parsing also works in Capturing Rules.