0

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":[]}
Lalindu
  • 329
  • 3
  • 17

2 Answers2

1

try JSON.stringify(object) would be hleped you & change the code to var tempBody : any ={};

var data= [
      {
        "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"
          }

        ]
      }

];
console.log(JSON.stringify(data));
holi-java
  • 29,655
  • 7
  • 72
  • 83
  • Hi @holi-java , Thanks a lot , but I need to generate it dynamically. I have updated the question to elaborate. – Lalindu Mar 09 '17 at 13:01
  • because `tempBody` is an array,so you can't serializing mixed type object,you can think how a serializer to serialize an array mixin properties.serialize it as an array?or as an object?so you must change the `var tempBody : any =[];` to `var tempBody : any ={};` – holi-java Mar 09 '17 at 13:28
  • Hi @holi-java , Thanks a lot implementing it as array solved my problem – Lalindu Mar 09 '17 at 13:47
1

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));
Stefan Schneider
  • 593
  • 5
  • 10