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
Newsletter
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?
  • Diagnosing Test Failures at Scale: Correlating Execution Evidence Across Ephemeral Runners

Trending

  • Google Cloud KMS vs. AWS KMS: Java Encryption Guide
  • Apache Phoenix: Global Secondary Indexes With Tunable Consistency
  • MQTT, Kafka, NATS, or Zenoh? Choosing Edge-to-Cloud Messaging by Traffic Pattern
  • A Field Guide to AI Agent Frameworks

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see 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
·
Updated Mar. 05, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
15.5K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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?
  • Diagnosing Test Failures at Scale: Correlating Execution Evidence Across Ephemeral Runners

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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