-2

I have json schema:

var data_json   {
"protocol": {
    "protocol_descr": {
        "protocol_name": "test"
    },
    "protocol_body": {
        "group": {
            "group_name": "test group",
            "position_info": {
                "position_name": "1",
                "position_type": "tags",
                "position_value": "some,value",
                "data-free":"false",
                "data-type":"false",
                "data-optional":"false"
            }
        }
    }
}
}

How add one more group to "protocol_body" ? I try to use next construction - data_json.protocol.protocol_body.push(), but it's not work.

Madushko
  • 113
  • 1
  • first, you need to make an array to protocol_body then only you can keep more than one group information inside that – nitishk72 May 17 '18 at 06:03
  • 1
    To be able to use `.push()`, at least one of the Objects needs to be wrapped or replaced by an Array ([Javascript Object push() function](https://stackoverflow.com/questions/8925820/javascript-object-push-function?lq=1)). Or, you'll need to assign a different key than the `"group"` already in place ([How can I add a key/value pair to a JavaScript object?](https://stackoverflow.com/questions/1168807/how-can-i-add-a-key-value-pair-to-a-javascript-object)). – Jonathan Lonowski May 17 '18 at 06:07
  • Notes: [Javascript object Vs JSON](https://stackoverflow.com/questions/8294088/javascript-object-vs-json) and [Is jQuery considered a language?](https://stackoverflow.com/questions/4201968/is-jquery-considered-a-language) – Jonathan Lonowski May 17 '18 at 06:12
  • @JonathanLonowski thank you! i solved my problem – Madushko May 17 '18 at 10:24

3 Answers3

0

You can directly assign object with assigning value object to key value of json object

Check below working snippet

var data_json  = {
"protocol": {
    "protocol_descr": {
        "protocol_name": "test"
    },
    "protocol_body": {
        "group": {
            "group_name": "test group",
            "position_info": {
                "position_name": "1",
                "position_type": "tags",
                "position_value": "some,value",
                "data-free":"false",
                "data-type":"false",
                "data-optional":"false"
            }
        }
    }
}
}
data_json.protocol.protocol_body.grop2 = {
  "group_name": "test group",
        "position_info": {
            "position_name": "1",
            "position_type": "tags",
            "position_value": "some,value",
            "data-free":"false",
            "data-type":"false",
            "data-optional":"false"
        }
}
console.log(data_json);
Nirali
  • 1,776
  • 2
  • 12
  • 23
0

You should change the group property to array type instead of object so that you can push more groups in that property. Something like this:

var data_json = {
  "protocol": {
      "protocol_descr": {
        "protocol_name": "test"
      },
      "protocol_body": {
        "group": [{
            "group_name": "test group",
            "position_info": {
                "position_name": "1",
                "position_type": "tags",
                "position_value": "some,value",
                "data-free":"false",
                "data-type":"false",
                "data-optional":"false"
            }
        }]
    }
  }
};

var newGroup = {
   "group_name": "test group2",
    "position_info": {
        "position_name": "2",
        "position_type": "tags2",
        "position_value": "some,value",
        "data-free":"false",
        "data-type":"false",
        "data-optional":"false"
    }
};

data_json.protocol.protocol_body.group.push(newGroup);
console.log(data_json);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
0

in your data "protocol_body" is not an array thats a reason it doesn't support push. you can use this.

data_json.protocol.protocol_body["Group2"]={"New Group":{"group_name": "test group2"}}
Negi Rox
  • 3,828
  • 1
  • 11
  • 18