I need help in dynamically constructing JSON using Javascript.
{
"Events": [{
"Name": "Code Change",
"Enabled": "true",
"Properties": [{
"Name": "url",
"Value": "val"
}]
}],
"Properties": [{
"Name": "url",
"Value": "val"
}]
}
The code I have written creates JSON with separate curly brackets for Name, Enabled, and properties. Is there a way to resolve this without using the push method?
Here is the code:
var eventProperties="[{'Name':'url','Value':'val'}]";
var subscriptionProperties="[{'Name':'url','Value':'val'}]";
var eventArray = JSON.parse('[1, 5, "false"]');
var subArray = JSON.parse('[1, 5, "false"]');
var subscription = {
Events: [],
Properties: []
};
if(eventName != null && eventName != "") {
subscription.Events.push({
"Name" : eventName
});
}
var index = 0;
if(eventEnabled != null && eventEnabled != "") {
subscription.Events.push({
Enabled: eventEnabled
});
}
if(eventProperties != null && eventProperties != "") {
subscription.Events.push({
"Properties": eval('(' + eventProperties + ')')
});
}
if(subscriptionProperties != null && subscriptionProperties != "") {
subscription.Properties = eval('(' + subscriptionProperties + ')');
}
Current Output:
{
"Events": [{
"Name": "Code Change"
},
{
"Enabled": "true"
},
{
"Properties": [{
"Name": "url",
"Value": "val"
}]
}],
"Properties": [{
"Name": "url",
"Value": "val"
}]
}