OiO.lk Blog java Groovy Builder- Unable to construct payload while inserting JSONArray
java

Groovy Builder- Unable to construct payload while inserting JSONArray


I was struggling to construct a JSON payload using Groovy Builder .

Expected JSON payload

{
  "subscriptions": [
    {
      "subscriptionsId": "1243232",
      "subscriptionName": "Employee",
      "subscribedCountries": [
        {
          "id": "901f5e1d-1139-49f1-a91e-6324232151bf",
          "name": "${countryName}",
          "countryId": "${countryId}"
        }
      ]
    }
  ]
}

In one JSON variable json2 , I am constructing the JSON array using the retrieved values ${countryName} ,${countryId}
Then inserting above Json array in my actual json payload json1 .

Problem – I am unable to insert the json array to my actual json payload

Please see below code

import groovy.json.JsonBuilder;
def builder = new JsonBuilder();

def json2=builder {}
json2.put('subscribedCountries',[]);
for(int i=2;i<=20;i++){
    json2.subscribedCountries.add([
    currencyId:vars.get('currencyId_'+i),
    countryId:vars.get('countryId_'+i),
    status:'Active'
    ])
}

def json1=builder {}
json1.subscriptions.add([
'subscriptionsId':'1243232',
'subscriptionName':'Employee',
***json2***         // Getting error here . Not sure how to implement 
])



You need to sign in to view this answers

Exit mobile version