I've got the following object:
var data = {
"parameter": []
};
I want to add an element to it like so, preserving the quotes on the keys as well as the escapes within the string values:
data.parameter.push({
"name": "TIBCO_Copy",
"value": "[{\"Source_Server\":[" + tibcoCopyJobs.map(function (i) { return i.sourceServer; }).join(',') +
"]},{\"Source_Path\":[" + tibcoCopyJobs.map(function (i) { return i.sourcePath; }).join(',') +
"]},{\"Destination_Server\":[" + tibcoCopyJobs.map(function (i) { return i.destinationServer; }).join(',') +
"]},{\"Destination_Path\":[" + tibcoCopyJobs.map(function (i) { return i.destinationPath; }).join(',') + "]}]"
});
Expected result:
{
"parameter": [
{
"name": "TIBCO_Copy",
"value": "[{\"Source_Server\":[1s, 2s, 3s]},{\"Source_Path\":[1sp, 2sp, 3sp]},{\"Destination_Server\":[1d, 2d, 3d]},{\"Destination_Path\":[1dp, 2dp, 3dp]}]"
}
]
}
Actual result as seen in chrome inspector:
name: "TIBCO_Copy",
value: "[{"Source_Server":[1,5]},{"Source_Path":[2,6]},{"Destination_Server":[3,7]},{"Destination_Path":[4,8]}]"
Notice that it interpreted the escaped quotes around the nested keys as well as the regular quotes on the top-level keys. Using JSON.stringify()
preserves the escapes, but adds another set of quotes, making it:
name: "TIBCO_Copy",
value: ""[{\"Source_Server\":[1,5]},{\"Source_Path\":[2,6]β¦on_Server\":[3,7]},{\"Destination_Path\":[4,8]}]""
In all of these though, I still have the issue of the name
and value
keys being interpreted since I can't seem to escape them.
I need it to be formatted this way because it gets POSTed to Jenkins to trigger a remote build, and then sent to Ansible via the Build over SSH plugin to be executed with Ansible's CLI. Thus the escaping is necessary for the data to be in the correct format when it reaches its destination.