How to parse and loop through JSON response body from an API call

I’m new to Katalon and still learning how it works. I’m calling an API that returns the response object below. I would like to parse and loop through each date attribute to verify it but I can’t figure out how to. Appreciate your help.

1 Like

Hi,

Welcome to our community. I found this parse and loop guide: Parse JSON responses with Katalon Studio | by Katalon | Katalon | Medium. Please follow it

Thanks for the response.

However, when I set the date key it shows as greyed out and I don’t get any value.

can you try this example

import static com.kms.


katalon.core.checkpoint.CheckpointFactory.findCheckpoint

import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase

import static com.kms.katalon.core.testdata.TestDataFactory.findTestData

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import static com.kms.katalon.core.testobject.ObjectRepository.findWindowsObject

import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint

import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW

import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile

import com.kms.katalon.core.model.FailureHandling as FailureHandling

import com.kms.katalon.core.testcase.TestCase as TestCase

import com.kms.katalon.core.testdata.TestData as TestData

import com.kms.katalon.core.testng.keyword.TestNGBuiltinKeywords as TestNGKW

import com.kms.katalon.core.testobject.TestObject as TestObject

import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

import com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows

import internal.GlobalVariable as GlobalVariable

import org.openqa.selenium.Keys as Keys

import groovy.json.JsonSlurper

// Sample JSON string (you would replace this with the actual response body from your API call)

// Assuming jsonResponse is a String containing your JSON response

String jsonResponse = '''

{

"dates": [

{"date": "2023-01-01"},

{"date": "2023-01-02"},

{"date": "2023-01-03"}

]

}

'''

// Parse the JSON response

try {

JsonSlurper slurper = new JsonSlurper()

def response = slurper.parseText(jsonResponse)

// Now 'response' is a Groovy map containing your JSON data

// Assuming 'response' contains the parsed JSON

response.dates.each { dateObj ->

// 'dateObj' is now each individual date object in the list

println "Date found: ${dateObj.date}"


}

}

catch (Exception e) {

println "Error parsing JSON: ${e.message}"

}

this will find all elements in the array and if you want to find

    def firstDateObj = response.dates[0] // Accessing the first element
    String firstDate = firstDateObj.date
    println "First date found: $firstDate"

good luck

1 Like