-3

I have JSON object like at below; And I want to add new Label 5 and Label 6 Element to my Json. I used this commands but i am getting this error : Cannot find function push in object [object Object]

I found an example from here ; Adding a new array element to a JSON object

I tried this example on my application, it is working but my json is not working.

var jsonStr = 'Json bla bla. you can see at below';
    var obj  = JSON.parse(jsonStr);
    obj['reservationNetworks'].push({"type": "complex","componentTypeId": "nw.service","componentId": null,"classId": "Reservation.Network","typeFilter": null,"values": {"entries": [{"key": "networkPath","value": {"type": "entityRef","classId": "Network","id": "6afec529-7fbc-45b7-8ac5-6abf944946ce","componentId": null,"label": "Label 5"}},{"key": "networkProfile","value": {"type": "entityRef","classId": "networkProfile","id": "187014d4-62f6-434c-a4da-c3e262d25ed4","componentId": null,"label": "Label 6"}}]}});
    jsonStr = JSON.stringify(obj);
    {
                "key": "reservationNetworks",
                "value": {
                    "type": "multiple",
                    "elementTypeId": "COMPLEX",
                    "items": [
                        {
                            "type": "complex",
                            "componentTypeId": "nw.service",
                            "componentId": null,
                            "classId": "Reservation.Network",
                            "typeFilter": null,
                            "values": {
                                "entries": [
                                    {
                                        "key": "networkPath",
                                        "value": {
                                            "type": "entityRef",
                                            "classId": "Network",
                                            "id": "6afec529-7fbc-45b7-8ac5-6abf944946ce",
                                            "componentId": null,
                                            "label": "Label 1"
                                        }
                                    },
                                    {
                                        "key": "networkProfile",
                                        "value": {
                                            "type": "entityRef",
                                            "classId": "networkProfile",
                                            "id": "187014d4-62f6-434c-a4da-c3e262d25ed4",
                                            "componentId": null,
                                            "label": "Label 2"
                                        }
                                    }
                                ]
                            }
                        },
{
                            "type": "complex",
                            "componentTypeId": "nw.service",
                            "componentId": null,
                            "classId": "Reservation.Network",
                            "typeFilter": null,
                            "values": {
                                "entries": [
                                    {
                                        "key": "networkPath",
                                        "value": {
                                            "type": "entityRef",
                                            "classId": "Network",
                                            "id": "6afec529-7fbc-45b7-8ac5-6abf944946ce",
                                            "componentId": null,
                                            "label": "Label 3"
                                        }
                                    },
                                    {
                                        "key": "networkProfile",
                                        "value": {
                                            "type": "entityRef",
                                            "classId": "networkProfile",
                                            "id": "187014d4-62f6-434c-a4da-c3e262d25ed4",
                                            "componentId": null,
                                            "label": "Label 4"
                                        }
                                    }
                                ]
                            }
                        }
                    ]
                }
            }
melpomene
  • 84,125
  • 8
  • 85
  • 148
Legioon
  • 21
  • 2
  • 9

1 Answers1

1

It's a bit hard to tell, but I guess that you want to add the data you are trying to push to the items array. You can do that by specifying the correct "path":

obj.value.items.push(...)

var obj = JSON.parse(jsonStr);
obj.value.items.push({
  "type": "complex",
  "componentTypeId": "nw.service",
  "componentId": null,
  "classId": "Reservation.Network",
  "typeFilter": null,
  "values": {
    "entries": [{
      "key": "networkPath",
      "value": {
        "type": "entityRef",
        "classId": "Network",
        "id": "6afec529-7fbc-45b7-8ac5-6abf944946ce",
        "componentId": null,
        "label": "Label 5"
      }
    }, {
      "key": "networkProfile",
      "value": {
        "type": "entityRef",
        "classId": "networkProfile",
        "id": "187014d4-62f6-434c-a4da-c3e262d25ed4",
        "componentId": null,
        "label": "Label 6"
      }
    }]
  }
});
console.log( JSON.stringify(obj) );
<script>
  const jsonStr = `{
    "key": "reservationNetworks",
    "value": {
      "type": "multiple",
      "elementTypeId": "COMPLEX",
      "items": [{
          "type": "complex",
          "componentTypeId": "nw.service",
          "componentId": null,
          "classId": "Reservation.Network",
          "typeFilter": null,
          "values": {
            "entries": [{
                "key": "networkPath",
                "value": {
                  "type": "entityRef",
                  "classId": "Network",
                  "id": "6afec529-7fbc-45b7-8ac5-6abf944946ce",
                  "componentId": null,
                  "label": "Label 1"
                }
              },
              {
                "key": "networkProfile",
                "value": {
                  "type": "entityRef",
                  "classId": "networkProfile",
                  "id": "187014d4-62f6-434c-a4da-c3e262d25ed4",
                  "componentId": null,
                  "label": "Label 2"
                }
              }
            ]
          }
        },
        {
          "type": "complex",
          "componentTypeId": "nw.service",
          "componentId": null,
          "classId": "Reservation.Network",
          "typeFilter": null,
          "values": {
            "entries": [{
                "key": "networkPath",
                "value": {
                  "type": "entityRef",
                  "classId": "Network",
                  "id": "6afec529-7fbc-45b7-8ac5-6abf944946ce",
                  "componentId": null,
                  "label": "Label 3"
                }
              },
              {
                "key": "networkProfile",
                "value": {
                  "type": "entityRef",
                  "classId": "networkProfile",
                  "id": "187014d4-62f6-434c-a4da-c3e262d25ed4",
                  "componentId": null,
                  "label": "Label 4"
                }
              }
            ]
          }
        }
      ]
    }
  }`;
</script>
baao
  • 71,625
  • 17
  • 143
  • 203