1

I have a json structure in a variable say "data" which is like this

{
"SearchWithMasterDataDIdAndScandefinitionDAO": [
    {
        "dateDm_id": 20120602,
        "issueValue": "ELTDIWKZ",
        "scanName": "Company Stored as Person (Given Name)",
        "severityCode": 1,
        "severityName": "High",
        "totalDiscovered": 1
    },
    {
        "dateDm_id": 20120602,
        "issueValue": "LTDFPVOI",
        "scanName": "Company Stored as Person (Given Name)",
        "severityCode": 1,
        "severityName": "High",
        "totalDiscovered": 1
    },
    {
        "dateDm_id": 20121005,
        "issueValue": "LTDILWGY",
        "scanName": "Company Stored as Person (Given Name)",
        "severityCode": 1,
        "severityName": "High",
        "totalDiscovered": 1
    },
    {
        "dateDm_id": 20121005,
        "issueValue": "YMORCLTD",
        "scanName": "Company Stored as Person (Given Name)",
        "severityCode": 1,
        "severityName": "High",
        "totalDiscovered": 1
    }
]
}

Now i want to change this structure to something like this where datedm_id from each object becomes the root of new json structure with array as value New wanted structure :

{
"20120602": [
    {
        "issueValue": "ELTDIWKZ",
        "scanName": "Company Stored as Person (Given Name)",
        "severityCode": 1,
        "severityName": "High",
        "totalDiscovered": 1
    },
    {
        "issueValue": "LTDFPVOI",
        "scanName": "Company Stored as Person (Given Name)",
        "severityCode": 1,
        "severityName": "High",
        "totalDiscovered": 1
    }
],
"20121005": [
    {
        "issueValue": "YMORCLTD",
        "scanName": "Company Stored as Person (Given Name)",
        "severityCode": 1,
        "severityName": "High",
        "totalDiscovered": 1
    },
    {
        "issueValue": "COOPER",
        "scanName": "Company Stored as Person (Given Name)",
        "severityCode": 1,
        "severityName": "High",
        "totalDiscovered": 15
    }
]
}

please help me with this

Saurabh Sinha
  • 1,772
  • 4
  • 27
  • 54
  • Good question. This is be useful for me! – Arthur Yakovlev Jan 28 '14 at 08:25
  • Please note that this question has **nothing** whatsoever to do with JSON. Once you parsed the JSON you are working with regular JavaScript objects and arrays. So it looks like you are asking for how to create a new object and how to iterate over objects/arrays. Please see [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/q/11922383/218196) and the [MDN JavaScript Guide - Working with Objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects). – Felix Kling Jan 28 '14 at 08:32
  • I dont have any problem in accessing the json structure, I just want to change the existing json structure to the modified one – Saurabh Sinha Jan 28 '14 at 08:34

2 Answers2

1

See the fiddle: http://jsfiddle.net/YVB2Y/

In summary you need to create an object inside the for loop.

var returnVar = {}
b.SearchWithMasterDataDIdAndScandefinitionDAO.forEach(function(item){
    var thisItem;
    if(returnVar[item.dateDm_id] == undefined){
        thisItem = [];
        returnVar[item.dateDm_id]  = thisItem;
    }
    else {
        thisItem = returnVar[item.dateDm_id];
    }

    var obj = {};

    obj.issueValue = item.issueValue;
    //and so on..

    thisItem.push(obj);

});

console.log(returnVar);
jacquard
  • 1,307
  • 1
  • 11
  • 16
0

underscore.js is perfect for dealing with complex data structures:

var json = { ... };
var result = _.groupBy(json['SearchWithMasterDataDIdAndScandefinitionDAO'], 
    function (iterator) {
        var key = iterator['dateDm_id'];
        delete iterator['dateDm_id'];

        return key;
    });

console.log(result);
Alon Gubkin
  • 56,458
  • 54
  • 195
  • 288