How to change json data in Angularjs

Hello all,

I have following data as a JSON in one variable which I am building in one controller. I can access this json data in other controller using factory/service. Now I want to modify this json data as like output json data.

Input Json

[  
   {  
      "text":"Identity",
      "checked":true,
      "timestamp":1435862483093
   },
   {  
      "text":"Calendar",
      "checked":true,
      "timestamp":1435862483443
   },
]

Output Json

{  
   "myname":{  
      "Facebook":{  
         "trackdata":[  
            {  
               "text":"Identity",
               "checked":true,
               "timestamp":1435862483093
            },
            {  
               "text":"Calendar",
               "checked":true,
               "timestamp":1435862483443
            }
         ],
         "selecteddata":[  
            {  
               "text":"Identity",
               "checked":true,
               "timestamp":1435862483093
            },
            {  
               "text":"Calendar",
               "checked":true,
               "timestamp":1435862483443
            }
         ]
      }
   }
}

How Can I construct this type of output in angular js ?

Thanks for your time.

This actually is not Angular specific, it is about creating multidimensional objects and arrays in javascript. Google for it and you will find a lot of documentation on it. To help you out, here is how I would do it (you can test with JSON.stringify which will convert your arrays and objects to a JSON string).

You are mixing objects with arrays in your JSON, that is why “trackdata” and “selecteddata” are using push (because those are arrays).

var root = {};
root['myName'] = {};
root['myName']['Facebook'] = {};
root['myName']['Facebook']['trackdata'] = [];
root['myName']['Facebook']['trackdata'].push({text:"Identify", checked: true, timestamp: 1435862483093});
root['myName']['Facebook']['trackdata'].push({text:"Calendar", checked: true, timestamp: 1435862483443});
root['myName']['Facebook']['selecteddata'] = [];
root['myName']['Facebook']['selecteddata'].push({text:"Identify", checked: true, timestamp: 1435862483093});
root['myName']['Facebook']['selecteddata'].push({text:"Calendar", checked: true, timestamp: 1435862483443});
console.log(JSON.stringify(root));