Add values to json object

Eash1
Tera Expert

Hello,

I am trying to figure out a way to add values to below Json object

Eg Json object - 

 

 


   "kind":"tm:ltm:data-group:internal:internalstate",
   "name":"smtp_whitelist",
   "fullPath":"smtp_whitelist",
   "generation":1,
   "selfLink":"https://localhost/mgmt/tm/ltm/data-group/internal/smtp_whitelist?ver=13.1.1.4",
   "type":"ip",
   "records":
      
         "name":"10.1.1.1/32",
         "data":""
      },
      
         "name":"192.168.1.1/32",
         "data":""
      },
      
         "name":"192.168.1.2/32",
         "data":""
      },
      
         "name":"192.168.2.0/24",
         "data":""
      }
   ]
}

 

trying to figure out a way to add new set of values like below under "records":

{

"name":"test",
         "data":"test"

}

 

I am able to iterate using below code but unable to add

gs.print("code: " + httpResponseStatus);
gs.print("body: " + JSON.parse(response.getBody()));
var b = JSON.parse(response.getBody())

var data = b.records;

for (var i in data)
{
var id = data[i].name;
var name = data[i].data;

gs.print("body 1: " + id);
gs.print("body 1: " + name);
}

1 ACCEPTED SOLUTION

Manish Vinayak1
Tera Guru

Hello,

You can add another set of values by pushing another object into that "records" array, i.e.

gs.print("code: " + httpResponseStatus);
gs.print("body: " + JSON.parse(response.getBody()));
var b = JSON.parse(response.getBody())

var data = b.records;

//Just an example, you could also push the details directly to b.records variable
//and can then convert b variable back to JSON string if that's what you wanted to do
data.push({"name" : "test", "data" : "test"});


for (var i in data)
{
var id = data[i].name;
var name = data[i].data;

gs.print("body 1: " + id);
gs.print("body 1: " + name);
}

That would add another set of values / JSON object in the records array.

Hope this helps!

Cheers,

Manish

View solution in original post

1 REPLY 1

Manish Vinayak1
Tera Guru

Hello,

You can add another set of values by pushing another object into that "records" array, i.e.

gs.print("code: " + httpResponseStatus);
gs.print("body: " + JSON.parse(response.getBody()));
var b = JSON.parse(response.getBody())

var data = b.records;

//Just an example, you could also push the details directly to b.records variable
//and can then convert b variable back to JSON string if that's what you wanted to do
data.push({"name" : "test", "data" : "test"});


for (var i in data)
{
var id = data[i].name;
var name = data[i].data;

gs.print("body 1: " + id);
gs.print("body 1: " + name);
}

That would add another set of values / JSON object in the records array.

Hope this helps!

Cheers,

Manish