-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.

4

3 Answers 3

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);

Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

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"}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.