Reputation: 105
Hello I want to simulate this scenario , example if I got this response below, I want to use that on my succeeding request. The appid will be use for key and Role to value parameter. Your response is highly appreciated. Thank you so much.
Request:
[{
"appid": "4a157c66-4965-30b2-af47-7dc68651c350",
"orgId": "205d64ff-9b58-3aad-bf48-29ec41dd4ef0",
"ipAddress": "192.168.100.235",
"Role" : "AD SERVER"},
{
"appid": "ba3fe4b4-1307-38e4-ace4-1636f8ffef3e",
"orgId": "205d64ff-9b58-3aad-bf48-29ec41dd4ef0",
"ipAddress": "192.168.100.236",
"Role" : "TESTING SERVER"
}, {
"appid": "29b59fbb-ea93-3adf-ba9a-fd353abaac21",
"orgId": "205d64ff-9b58-3aad-bf48-29ec41dd4ef0",
"ipAddress": "192.168.100.237",
"Role": "UAT SERVER"
}]
Response Screenshot:
Expected Result:
Upvotes: 0
Views: 464
Reputation: 1841
This could be another solution. There should be a better way to replace the keys
import groovy.json.JsonGenerator
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
String response = """
[{
"appid": "4a157c66-4965-30b2-af47-7dc68651c350",
"orgId": "205d64ff-9b58-3aad-bf48-29ec41dd4ef0",
"ipAddress": "192.168.100.235",
"Role" : "AD SERVER"},
{
"appid": "ba3fe4b4-1307-38e4-ace4-1636f8ffef3e",
"orgId": "205d64ff-9b58-3aad-bf48-29ec41dd4ef0",
"ipAddress": "192.168.100.236",
"Role" : "TESTING SERVER"
}, {
"appid": "29b59fbb-ea93-3adf-ba9a-fd353abaac21",
"orgId": "205d64ff-9b58-3aad-bf48-29ec41dd4ef0",
"ipAddress": "192.168.100.237",
"Role": "UAT SERVER"
}]
"""
def jsonSlurper = new JsonSlurper()
def responseJSON = jsonSlurper.parseText(response)
def generator = new JsonGenerator.Options()
.excludeNulls()
.excludeFieldsByName('orgId', 'ipAddress')
.build()
String request = generator.toJson(responseJSON)
request =JsonOutput.prettyPrint(request)
request = request.replace("appid", "key")
.replace("Role","value")
println(request)
Upvotes: 0
Reputation: 1841
You could use JSON JMSEPath extractor to extract values and transform the keys. JMESPath Tutorial
[].{Key:Role,Value:orgId}
Following demonstrate the testing the JSON JMESPath Query
in the View Results Tree.
Upvotes: 1
Reputation: 1841
Here is a quick solution with Groovy.
String response = """
[{
"appid": "4a157c66-4965-30b2-af47-7dc68651c350",
"orgId": "205d64ff-9b58-3aad-bf48-29ec41dd4ef0",
"ipAddress": "192.168.100.235",
"Role" : "AD SERVER"},
{
"appid": "ba3fe4b4-1307-38e4-ace4-1636f8ffef3e",
"orgId": "205d64ff-9b58-3aad-bf48-29ec41dd4ef0",
"ipAddress": "192.168.100.236",
"Role" : "TESTING SERVER"
}, {
"appid": "29b59fbb-ea93-3adf-ba9a-fd353abaac21",
"orgId": "205d64ff-9b58-3aad-bf48-29ec41dd4ef0",
"ipAddress": "192.168.100.237",
"Role": "UAT SERVER"
}]
"""
String request = response.replace("appid", "key")
.replace("Role", "value")
.replaceAll("\"orgId\":\\s?\".*\",\n", "")
.replaceAll("\"ipAddress\":\\s?\".*\",\n", "")
Upvotes: 0