syed naveed
syed naveed

Reputation: 95

Java illegal argument exception when setting Json value using groovy

I am trying to set 'code' value as 'Test2' for given Json using groovy but i get Java illegal argument exception while setting the value.

Raw request:

{
    "langauageCode": "en-US",
    "Test": [{
        "_modificationTypeCode": "added",
        "allocationTypeCode": "3",
        "code": "Test1"
    }]
}

Here is the code I am using

def jsonRequest = slurper.parseText(rawRequest)
def builder = new JsonBuilder(jsonRequest)
builder.content.Test.code ='Test2' //Throwing java illegal argument but when I print using log.info I get the value
 log.info("testbuilder " + builder.content.Test.code)

Can some one please let me know while I am setting the value why I am getting Java illegal argument exception?

Upvotes: 0

Views: 174

Answers (1)

Gal Naor
Gal Naor

Reputation: 2397

It's because Test is an array. if you want to set the code, you need to:

builder.content.Test[0].code = 'Test2'

Upvotes: 1

Related Questions