Forum Discussion

JackMcC10's avatar
New Contributor
8 years ago

How to dynamically change JSON elements within Groovy Script

I want to change two JSON elements each time I send a request within SoapUI.

I have my request setup within test properties and each time the test case is ran a new email address and postal address should be created in order to create unique users. My code is as follows:



import groovy.json.JsonSlurper
import groovy.json.JsonOutput
import groovy.json.JsonBuilder

def props = new java.util.Properties();
props = testRunner.testCase.testSuite.project.testSuites["Avios"].testCases["SuccessJoin"].testSteps["Properties"];

def username = System.currentTimeMillis()
def email = username + ""

def random = Math.abs(new Random().nextInt() % 600) + 1
def address = random + " Swords manor"


def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText'''{

"13120 River valley drive",
}''' = email
object.member.person.postalAddresses.preferredPostalAddress.addressLine = address

def jsonReqAsString = JsonOutput.toJson(object)




I get an error when I run this saying:


org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token\n


Any help would be greatly appreciated.


6 Replies

  • 01streams's avatar
    New Contributor

    I don't see any problem with your JSON parse/update script as such. When I commented the lines where you are "trying to read" the properties and the lines where you are setting the JSON into the test step property, I get the below expected JSON printed in the log.


    Also something that I noticed from the commented lines - you are overwriting the "props" variable  that was initially defined as a Java property with a reference of the 'Properties' test step, not sure if that was intentional.


    {"member":{"person":{"dateOfBirth":"1990-12-15","locale":{"languageCode":"en"},"countryOfResidence":"US","name":{"title":"Mr","familyName":"SMITH","middleInitial":["L"],"type":"MARRIED_NAME","firstName":"Jack"},"gender":"MALE","postalAddresses":{"preferredPostalAddress":{"addressLine":"339 Swords manor","type":"PERSONAL","placeType":"NOT_KNOWN","country":"US"}},"telecomAddresses":{"preferredTelecomAddress":{"areaCode":"087","deviceType":"MOBILE","number":"1234560","type":"PERSONAL"}},"emailAddresses":{"preferredEmailAddress":{"email":"","mailboxType":"INDIVIDUAL","type":"BUSINESS"}}}}}

    • JackMcC10's avatar
      New Contributor

      If I was just sending the request there would be no issue, I am making reference to two specific elements in the request which need changed which is where the issue arises. I am setting a property within the test case called NextRequestJson which does not like when I change these elements.


      My question is how to change these elements- postal address and email address- and not get a bad request exception.

      • 01streams's avatar
        New Contributor

        Instead of setting the new JSON into a request property and reading from it, you could try setting the JSON directly as the request to the next test step as in the snippet below -


        def nextTestStep = testRunner.testCase.testSuite.project.testSuites["Avios"].testCases["SuccessJoin"].testSteps["Properties"]


        // def object = (build the new JSON with the updated email and postal address)


        def jsonReqAsString = JsonOutput.toJson(object)

        nextTestStep .setPropertyValue('Request', jsonReqAsString);