Feather Background Waves Background
Skip to main content
Feather Background Waves Background
Feather Background Waves Background
  1. JMeter in English/

Groovy - JSONSlurper

Advanced - This article is part of a series.
Part 7: This Article

Groovy

What is JsonSlurper ?>

What is JsonSlurper ? #

JsonSlurper is a class of Groovy, used to manage files or strings in JSON format. It is part of the Groovy scripting library that provides an easy and fast way to handle JSON data. JsonSlurper converts the JSON content into a Groovy data structure, such as maps, lists, and objects, which can be manipulated and utilized similarly to any other object.

What is Groovy?>

What is Groovy? #

Groovy is a general-purpose programming language based on Java. It was developed as an easier-to-use and more expressive alternative to Java, designed for facilitating task automation and integration with other languages and platforms. Groovy offers a clearer and more concise syntax than Java, along with numerous dynamic programming features such as object-oriented programming, functional programming, and meta-programming. Additionally, Groovy can be run on the Java platform without requiring prior compilation, making it ideal for scripting and automation tasks. In my opinion, if Java and Python had a child, this would likely be Groovy.

What is JSON?>

What is JSON? #

JSON (JavaScript Object Notation) is a lightweight and easy-to-read text format for exchanging data. It is commonly used to transmit information between an application client and a server or to transmit data between applications. JSON represents data in the form of nested objects and lists of objects, and uses a clear and easy-to-read syntax based on keys { } to define objects and brackets [ ] to define lists.

The properties of a JSON object are represented as pairs of key and value separated by two dots : as can be seen in the following example:

{
  "name": "John Doe",
  "age": 35,
  "email": "john.doe@example.com"
}
How Does JSONSlurper Work in JMeter?>

How Does JSONSlurper Work in JMeter? #

JSONSlurper can be used in Apache JMeter to parse a JSON response from an HTTP request. Here is an example of Groovy code that shows how to use JSONSlurper in JMeter:

{
  "name": "John Doe",
  "age": 35,
  "address": {
    "street": "123 Main St",
    "city": "Anytown",
    "state": "WA"
  },
  "phoneNumbers": [
    {
      "type": "mobile",
      "number": "555-555-1234"
    },
    {
      "type": "home",
      "number": "555-555-5678"
    },
    {
      "type": "office",
      "number": "555-555-5111"
    },
    {
      "type": "virtual",
      "number": "555-555-3321"
    }
  ],
  "email": "john.doe@example.com"
}
  1. We added a JSR223 post-processor and introduced the following code:
import groovy.json.JsonSlurper

def jsonSlurper = new JsonSlurper()
def response = prev.getResponseDataAsString()
def json = jsonSlurper.parseText(response)

log.info(json.phoneNumbers[0].number)
  1. This would return the following in the JMeter log:
2021-08-29 16:01:00,972 INFO o.a.j.t.JMeterThread: Thread started: Thread Group 1-1
2021-08-29 16:01:01,969 INFO o.a.j.e.J.JSR223 PostProcessor: 555-555-1234
2021-08-29 16:01:01,970 INFO o.a.j.t.JMeterThread: Thread is done: Thread Group 1-1

jsonslurper-image1

The previous example could be simpler if we used a JSON extractor to extract one or more values and store them in memory, as shown in the images.

jsonslurper-image2

jsonslurper-image3

What other uses could JSONSlurper have in JMeter?>

What other uses could JSONSlurper have in JMeter? #

JSONSlurper is a very powerful tool for parsing and extracting a series of values. The previous example is easy to solve with JSONPath, but things get complicated when we need to extract multiple values from the response, as we would have to use a JSONPath for a value from different nodes. In the following example, we have multiple users: John, Peter, and Rick. Things start to get complicated.

[{
  "name": "John Doe",
  "age": 35,
  "address": {
    "street": "123 Main St",
    "city": "Anytown",
    "state": "WA"
  },
  "phoneNumbers": [
    {
      "type": "mobile",
      "number": "555-551-1234"
    },
    {
      "type": "home",
      "number": "555-552-5678"
    }
  ],
  "email": "john.doe@example.com"
},{
  "name": "Peter Doe",
  "age": 34,
  "address": {
    "street": "123 Second St",
    "city": "Anytown",
    "state": "TX"
  },
  "phoneNumbers": [
    {
      "type": "mobile",
      "number": "555-553-1234"
    },
    {
      "type": "home",
      "number": "555-554-5678"
    }
  ],
  "email": "peter.doe@example.com"
},{
  "name": "Rick Doe",
  "age": 36,
  "address": {
    "street": "123 Third St",
    "city": "Anytown",
    "state": "NJ"
  },
  "phoneNumbers": [
    {
      "type": "mobile",
      "number": "555-555-1234"
    },
    {
      "type": "home",
      "number": "555-556-5678"
    }
  ],
  "email": "rick.doe@example.com"
}]

For this example, it would be best to save the entire response in memory as a variable, then retrieve an object or node from the array (we have 3 nodes or users in the array: John, Peter, and Rick), then retrieve the first phone number for each node:

import groovy.json.JsonSlurper

def response = prev.getResponseDataAsString()
vars.put("response",response) //Save the previous JSON response in the response variable
def jsonSlurper = new JsonSlurper()
def json = jsonSlurper.parseText(response) //Process the JSON input in case it needs to be reused.

// -- Examples - //
json.each { //Iterate over the array of nodes
  log.info( it.phoneNumbers[0].toString() ) //Print the value of the first phone number entry
}

Other examples could be evaluating the age of the oldest user to obtain their phone number:

def oldestPerson = json.sort{ it.age }[-1] //We sort the array and obtain the last value.
log.info(oldestPerson.name)

We could also get the address of people who live in a certain state like Texas:

json.each {
  if(it.address.state.equals("TX")){ //We evaluate that the state of residence is Texas
    log.info(it.address.street)
  }
}

Finally, we could print all emails that contain a certain keyword:

json.each {
  if(it.email.contains("doe")){
    log.info(it.email)
  }
}
Conclusion>

Conclusion #

JSONSlurper is a very effective tool for substituting any JSON Extractor to obtain multiple values or evaluation of the same ones to satisfy one or more criteria, which could be very difficult to achieve with JSONPath query due to we can use native Groovy functions. The environment and handling of information within nodes becomes very simple and highly scalable, long time ago developed a criterion for evaluating multiple JSON files to reserve spaces, without the help of JSONslurper that would have become a very complex problem, but it is fast and can export the resulting object as a property in JMeter such as Lists or Maps, handling becomes simple and clean, obviously there’s much practice needed to obtain expected results. Until next time and I hope you enjoy the entry.



Advanced - This article is part of a series.
Part 7: This Article