Trying to figure out how can I insert a new row under net. Basically I need to figure out how many are already in there so I dont overlap it , and append a new one.
Original:
{
"jsonrpc": "2.0",
"method": "update",
"params": {
"vid": 10,
"_update": {
"data": {
"lan": {
"net": [
{
"id": 1,
"name": "FIRST"
},
{
"id": 15,
"name": "SECOND"
}
]
}
}
}
},
"id": 1
}
Wanted to add:
{
"id": 8,
"name": "THIRD"
}
So FINAL would be new
{
"jsonrpc": "2.0",
"method": "update",
"params": {
"vid": 10,
"_update": {
"data": {
"lan": {
"net": [
{
"id": 1,
"name": "FIRST"
},
{
"id": 15,
"name": "SECOND"
},
{
"id": 8,
"name": "THIRD"
}
]
}
}
}
},
"id": 1
}
To clarify: I will make a POST that retrieves a json data - which is not fixed, it may have more elements or less every time I run the POST.
I'd like to add something to this json and use it to POST it into another request.
I figure I could store the reply in a env.variable using TEST, that part works, then use this env.variable as a body in another POST this works as well
now I just need to figure out how to add something into the JSON I collected in my first POST, that is the original question :)
how is the third entry stored in the datafile?
If its stored as a string, i would suggest the pragmatic way: Just add the variable into the JSON Body(but don't forget the comma).
{
"jsonrpc": "2.0",
"method": "update",
"params": {
"vid": 10,
"_update": {
"data": {
"lan": {
"net": [
{
"id": 1,
"name": "FIRST"
},
{
"id": 15,
"name": "SECOND"
},
{{data_value}}
]
}
}
}
},
"id": 1
}
if the data is stored separately e.g. as id and name, just add this to your pre-request-script:
var myNewObject = { id : data.id, name:data.name};
pm.globals.set("myNewObject", JSON.stringify(myNewObject));
This snippet will add a new Object with id and name, and set it up with the data id and name from your datafile. It will be stored as a global variable myNewObject, which can be used in your body (don't forget the comma ;-) ):
{
"jsonrpc": "2.0",
"method": "update",
"params": {
"vid": 10,
"_update": {
"data": {
"lan": {
"net": [
{
"id": 1,
"name": "FIRST"
},
{
"id": 15,
"name": "SECOND"
},
{{myNewObject}}
]
}
}
}
},
"id": 1
}
An other idea can be to store the whole "net" array in a variable. You can append the new object to this array. But in this Case the whole "net" array must be known (e.g. From a global variable).
JsonBody
{
"jsonrpc": "2.0",
"method": "update",
"params": {
"vid": 10,
"_update": {
"data": {
"lan": {
"net": {{netArray}}
},
"id": 1
}
pre-request script
var myNewObject = { id : data.id, name:data.name};
var netArray = JSON.parse(pm.globals.get("netArray"));
netArray.push(myNewObject); //<-- Adds new object to the array
pm.globals.set("netArray", JSON.stringify(netArray));