JSON Path Usage for Gatling Tests

JSON Path Usage for Gatling Tests

As Loadium gives support to Gatling tool, we thought it would be great to give some tips about Gatling’s data extraction strategies. The…

JSON Path Usage for Gatling Tests

As Loadium gives support to Gatling tool, we thought it would be great to give some tips about Gatling’s data extraction strategies. The purpose of Gatling test is not different than Apache JMeter test, and that is performing a load test on your application. For a realistic performance test, you need to use dynamic data. To overcome this challenge, you need to extract data from a JSON, XML or HTML file. Let’s take a look at jsonpath expression usage with some examples.

How to Extract Data with JSONPATH

We are going to use JSON Placeholder web services for JSON Path example.

At first, we need to make an HTTP request to an endpoint to receive a JSON response. For that, we need to create a request to “/todos” endpoint and receive a response body as below.

We create an exec object that makes a get request to /todos. You can change the value in the HTTP object to change its name shown in the reports. When we run the test, our request will receive a response body like below:

Secondly, we need to extract the ids so we send a specific HTTP request to “todos/id”. So we change our HTTP request object as below to save all values extracted from JSON.

By using the check function, we are able to validate the response body. Then we call jsonPath method and finally save all id values in the taskIds variable. So we use it in another request.

Then we loop through all the values that we got from the first request by using for each function. In order to call a variable in your source code, you need to use ${variableName} structure.

Now you are ready to execute your parametrized Gatling test with as many virtual users as you want. Just upload your script on Loadium and monitor the results.