I am trying to serializing list to Json using TypeScirp but I couldn't find way to handle complex nested collection . Please be kind enough to show some light.
Below is the sample structure I am trying to convert.
[
{
"method": "PATCH",
"uri": "/ScrumStarain/_apis/wit/workItems/$Product Backlog Item?api-version=1.0",
"headers": {
"Content-Type": "application/json-patch+json"
},
"body": [
{
"op": "add",
"path": "/fields/System.Title",
"value": "apip1"
},
{
"op": "add",
"path": "/id",
"value": "-1"
}
]
},
{
"method": "PATCH",
"uri": "/ScrumStarain/_apis/wit/workItems/$Task?api-version=1.0",
"headers": {
"Content-Type": "application/json-patch+json"
},
"body": [
{
"op": "add",
"path": "/fields/System.Title",
"value": "apip2"
},
{
"op": "add",
"path": "/id",
"value": "-2"
}
]
}
]
below is the sample what I was trying to do , I am trying to generate it dynamically . I was failed to add body and subordinate tags.
var state =" xy" ;
var tagsCollection =["a","b"];
var tempBody : any =[];
tempBody.op = "add";
tempBody.path = "/fields/System.State";
tempBody.value = state;
var jsonMainString:any = {};
jsonMainString.method = "PATCH";
jsonMainString.uri="/_apis/wit/workItems/123?api-version=1.0";
jsonMainString.headers = { "Content-Type" :"application/json-patch+json"};
jsonMainString.body = tempBody;
console.log(JSON.stringify(jsonMainString));
and ultimate output looks like below , which is not correct
{"method":"PATCH","uri":"/_apis/wit/workItems/123?api-version=1.0","headers":{"Content-Type":"application/json-patch+json"},"body":[]}
You maybe should look up on how to manipulate Arrays in JavaScript. So I guess what you are trying is getting the body populated.
var state =" xy" ;
var tagsCollection =["a","b"];
var tempBody =[];
tempBody.push({
op: "add",
path: "/fields/System.State",
value: state
});
var jsonMainString = {};
jsonMainString.method = "PATCH";
jsonMainString.uri="/_apis/wit/workItems/123?api-version=1.0";
jsonMainString.headers = { "Content-Type" :"application/json-patch+json"};
jsonMainString.body = tempBody;
console.log(JSON.stringify(jsonMainString));