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

3
  • Good question. This is be useful for me! Commented Jan 28, 2014 at 8: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 and the MDN JavaScript Guide - Working with Objects. Commented Jan 28, 2014 at 8:32
  • I dont have any problem in accessing the json structure, I just want to change the existing json structure to the modified one Commented Jan 28, 2014 at 8:34

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.