-2

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": "rbarrows@dcloud.cisco.com"
    }]
  }]
}

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"}
Bourbia Brahim
  • 14,459
  • 4
  • 39
  • 52
Ganesh Putta
  • 2,622
  • 2
  • 19
  • 26
  • 3
    Please show us what you have tried, but solution should be something like `jsonname.agents[0].agent.push(newdata)` – Carsten Løvbo Andersen Jan 31 '23 at 11:45
  • Does this answer your question? [How can I access and process nested objects, arrays, or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json) – Johannes Stadler Jan 31 '23 at 11:47
  • 1
    "JSON" is just a string, so you would use `data.indexOf(...)` to find where and then `data = data.slice(0, pos) + extrajson + data.slice(pos)` (or so) - if you want to add it to the *javascript object* then just `agents.push(extraAgent)`. – freedomn-m Jan 31 '23 at 12:07
  • @GaneshPutta ... 1/3 ... In case the OP deals with [`JSON`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON)-data which is a serialized / stringified version of mostly a JavaScript [data-structure](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#properties), the OP needs to **first** [`parse`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) this string into an object like e.g. `const data = JSON.parse(serializedData)`, ... – Peter Seliger Jan 31 '23 at 14:06
  • @GaneshPutta ... 2/3 ... **second** [`push`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push) the `agent` data-item into the correct `data` sub-structure like e.g. `data.agents.agent.push(agentItem)`. At last the OP needs to serialize/[`stringify`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) the updated data-structure again like e.g. `serializedData = JSON.stringify(data);`. – Peter Seliger Jan 31 '23 at 14:08
  • @GaneshPutta ... 3/3 ... And in case the OP already has to process an object which the OP mistakingly refers to as `JSON`, then the OP just has to take care of the above mentioned `push` task. – Peter Seliger Jan 31 '23 at 14:08
  • 2
    @PeterSeliger, answers go down there. If you have to write chapter comments that's probably what it should be. Or close as the multi-duplicate this is. Note: Ganesh Putta _is_ the OP. – isherwood Jan 31 '23 at 14:20
  • You're expected to make an attempt and show your code. Please review [ask]. – isherwood Jan 31 '23 at 14:21

1 Answers1

0

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": "rbarrows@dcloud.cisco.com"
    }]
  }]
}

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)
Bourbia Brahim
  • 14,459
  • 4
  • 39
  • 52