Ganesh Putta
Ganesh Putta

Reputation: 2671

how to add another element to json

I have a json like this

{
  "refURL": "/unifiedconfig/config/agentteam/5022",
  "changeStamp": 12,
  "agentCount": 7,
  "description": "Cumulus Outbound Team",
  "name": "CumulusOutbound",
  "peripheral": {
    "id": 5000,
    "name": "CUCM_PG_1"
  },
  "peripheralId": 5000,
  "supervisorCount": 1,
  "agents": [{
    "agent": [{
      "refURL": "/unifiedconfig/config/agent/5197",
      "agentId": "1085",
      "firstName": "Owen",
      "lastName": "Harvey",
      "userName": "oharvey"
    }, {
      "refURL": "/unifiedconfig/config/agent/5201",
      "agentId": "1320",
      "firstName": "Bruce",
      "lastName": "Wayne",
      "userName": "bwayne"
    }, {
      "refURL": "/unifiedconfig/config/agent/5202",
      "agentId": "1321",
      "firstName": "Peter",
      "lastName": "Parker",
      "userName": "pparker"
    }, {
      "refURL": "/unifiedconfig/config/agent/5203",
      "agentId": "1322",
      "firstName": "Tony",
      "lastName": "Stark",
      "userName": "tstark"
    }, {
      "refURL": "/unifiedconfig/config/agent/5204",
      "agentId": "1323",
      "firstName": "Steve",
      "lastName": "Rogers",
      "userName": "srogers"
    }, {
      "refURL": "/unifiedconfig/config/agent/5205",
      "agentId": "1324",
      "firstName": "Bruce",
      "lastName": "Banner",
      "userName": "bbanner"
    }, {
      "refURL": "/unifiedconfig/config/agent/5231",
      "agentId": "1086",
      "firstName": "Annika",
      "lastName": "Hamilton",
      "userName": "annika"
    }]
  }],
  "supervisors": [{
    "supervisor": [{
      "refURL": "/unifiedconfig/config/agent/5174",
      "agentId": "1082",
      "firstName": "Rick",
      "lastName": "Barrows",
      "userName": "[email protected]"
    }]
  }]
}

To the above json, i need to add another agent inside of agents[] of agents[], below is the element that i need to add

{"refURL":"/unifiedconfig/config/agent/2255","agentId":"1478","firstName":"kkk","lastName":"ddd","userName":"dddd"}

Upvotes: -2

Views: 79

Answers (1)

Bourbia Brahim
Bourbia Brahim

Reputation: 14702

supposing you're dealing with a json object : in that case , all you have is just to access the correct json path then push the new json agent into the array of agents .

here the it should be like yourJsobObject.agents[0].agent

to insert New json just use push : yourJsobObject.agents[0].agent.push(newJsonAgent);

you can check below snippet as an example :

let json = {
  "refURL": "/unifiedconfig/config/agentteam/5022",
  "changeStamp": 12,
  "agentCount": 7,
  "description": "Cumulus Outbound Team",
  "name": "CumulusOutbound",
  "peripheral": {
    "id": 5000,
    "name": "CUCM_PG_1"
  },
  "peripheralId": 5000,
  "supervisorCount": 1,
  "agents": [{
    "agent": [{
      "refURL": "/unifiedconfig/config/agent/5197",
      "agentId": "1085",
      "firstName": "Owen",
      "lastName": "Harvey",
      "userName": "oharvey"
    }, {
      "refURL": "/unifiedconfig/config/agent/5201",
      "agentId": "1320",
      "firstName": "Bruce",
      "lastName": "Wayne",
      "userName": "bwayne"
    }, {
      "refURL": "/unifiedconfig/config/agent/5202",
      "agentId": "1321",
      "firstName": "Peter",
      "lastName": "Parker",
      "userName": "pparker"
    }, {
      "refURL": "/unifiedconfig/config/agent/5203",
      "agentId": "1322",
      "firstName": "Tony",
      "lastName": "Stark",
      "userName": "tstark"
    }, {
      "refURL": "/unifiedconfig/config/agent/5204",
      "agentId": "1323",
      "firstName": "Steve",
      "lastName": "Rogers",
      "userName": "srogers"
    }, {
      "refURL": "/unifiedconfig/config/agent/5205",
      "agentId": "1324",
      "firstName": "Bruce",
      "lastName": "Banner",
      "userName": "bbanner"
    }, {
      "refURL": "/unifiedconfig/config/agent/5231",
      "agentId": "1086",
      "firstName": "Annika",
      "lastName": "Hamilton",
      "userName": "annika"
    }]
  }],
  "supervisors": [{
    "supervisor": [{
      "refURL": "/unifiedconfig/config/agent/5174",
      "agentId": "1082",
      "firstName": "Rick",
      "lastName": "Barrows",
      "userName": "[email protected]"
    }]
  }]
}

let newAgent = {"refURL":"/unifiedconfig/config/agent/2255","agentId":"1478","firstName":"kkk","lastName":"ddd","userName":"dddd"}

json.agents[0].agent.push(newAgent);

console.log(json.agents[0].agent)

Upvotes: 0

Related Questions