DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Apache Spark 3 to Apache Spark 4 Migration: What Breaks, What Improves, What's Mandatory
  • What Is API-First?
  • How to Do API Testing?
  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever

Trending

  • 11 Agentic Testing Tools to Know in 2026
  • Scaling Cloud Data Automation: A Practical Guide to Open Table Formats
  • Lambda-Driven API Design: Building Composable Node.js Endpoints With Functional Primitives
  • Chaos Engineering Has a Blind Spot. Agentic AI Lives in It.
  1. DZone
  2. Coding
  3. Languages
  4. JSON Path Usage for Gatling Tests

JSON Path Usage for Gatling Tests

Learn more about how to extract data with jsonpath.

By 
Canberk Akduygu user avatar
Canberk Akduygu
·
Updated Mar. 05, 19 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
15.3K Views

Join the DZone community and get the full member experience.

Join For Free

As Loadium gives support to the Gatling tool, we thought it would be great to give some tips about Gatling’s data extraction strategies. The purpose of a 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.

object Task {
def get_task = exec(http("Get all task ids")
.get("/todos")
}


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:

[
  {
    "userId": 1,
    "id": 1,
    "title": "delectus aut autem",
    "completed": false
  },
  {
    "userId": 1,
    "id": 2,
    "title": "quis ut nam facilis et officia qui",
    "completed": false
  }
….
]


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.

object Task {

 def get_task = exec(http("Get all task id")
   .get("/todos")
   .check(jsonPath("$[*].id").findAll.saveAs("taskIds")))
}


By using the check function, we are able to validate the response body. Then, we call the 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 the ${variableName} structure.

object Task {

 def get_task = exec(http("Get aall task id")
   .get("/todos")
   .check(jsonPath("$[*].id").findAll.saveAs("taskIds")))
   .foreach("${taskIds}", "taskId") {
     exec(http("Get task info")
       .get("/todos/${taskId}")
     )}

}


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.

Go loadium.com and execute your Gatling tests

JSON Testing Gatling (software)

Published at DZone with permission of Canberk Akduygu. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Apache Spark 3 to Apache Spark 4 Migration: What Breaks, What Improves, What's Mandatory
  • What Is API-First?
  • How to Do API Testing?
  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook