1

I need to reorganize my JSON into new structure using Javascript/Jquery, but i have no idea how to do this.

My current JSON looks this like,

{
   'dishList': [
  {
   'dishName': 'bla-bla1',
   'dishNumber': 4,
    'dishType': 'Type 1'
  },
   {
   'dishName': 'bla-bla2',
   'dishNumber': 12,
   'dishType': 'Type 1'
  },
  {
   'dishName': 'bla-bla3',
   'dishNumber': 2,
   'dishType': 'Type 2'
  }
 ]
}

And i need following structure:

{
'dishList': [
    {
    'dishType': 'Type 1',
    'dishes': [
        {
         'dishName': 'bla-bla1',
         'dishNumber': 4
        },
        {
         'dishName': 'bla-bla2',
         'dishNumber': 12
        }
    ]
    },
    {
    'dishType': 'Type 2',
    'dishes': [
        {
         'dishName': 'bla-bla3',
         'dishNumber': 2
        }
        ]
    }
]
}

Any ideas how can i do this?

Thank you.

Mark Walters
  • 12,060
  • 6
  • 33
  • 48
you-rick
  • 185
  • 1
  • 3
  • 12
  • 4
    Take a look at this: [Re-structuring a JSON](http://stackoverflow.com/questions/21034128/re-structuring-a-json/21035282#21035282). – Beterraba Feb 19 '14 at 14:54

3 Answers3

4

The code below will restructure it for this specific case. I am sure there is a more efficient way to do this, and probably a more abstract way as well:

var orig_json = {
        "dishList": [{
            "dishName": "bla-bla1",
                "dishNumber": 4,
                "dishType": "Type 1"
        }, {
            "dishName": "bla-bla2",
                "dishNumber": 12,
                "dishType": "Type 1"
        }, {
            "dishName": "bla-bla3",
                "dishNumber": 2,
                "dishType": "Type 2"
        }]
    },
    new_json;
var reorg = function (data) {
    var types = [],
        dishes = [];
    // pass 1, add distinct types
    data.dishList.forEach(function (value, index, array) {
        if (types.indexOf(value.dishType) === -1) {
            // doesn't yet exist
            types.push(value.dishType);
        }
    });
    // for each distinct type
    types.forEach(function (value, index, array) {
        // pass two to n, reorganize based on type
        data.dishList.forEach(function (val, i, a) {
            if (val.dishType === value) {
                // matches dishType
                dishes.push({
                    "dishName": val.dishName,
                    "dishNumber": val.dishNumber
                });
            }
        });
        // redefine value of current array element
        array[index] = {
            "dishType": value,
            "dishes": dishes
        };
        // reset dishes array
        dishes = [];
    });
    return {
        "dishList": types
    };
};
new_json = reorg(orig_json);

Here's a fiddle: http://jsfiddle.net/NYhBM/

pete
  • 24,141
  • 4
  • 37
  • 51
2

Try this function:

var f = function(json){
    var obj = {};

    // put dishes into a temporary object
    // that has properties named as dish types
    // and its values are array of dish objects of that type
    for(var i=0;i<json.dishList.length;i++){
        if(!obj[json.dishList[i].dishType])
                obj[json.dishList[i].dishType] = [];
        obj[json.dishList[i].dishType].push({
            dishName: json.dishList[i].dishName,
            dishNumber: json.dishList[i].dishNumber,
        })
    }   
    var res = {
        dishList: []
    }

    // loop through properties of obj i.e. dish types
    // and populate the ouptput dishlist.
    for(var p in obj){
        if(obj.hasOwnProperty(p))
            res.dishList.push({
                dishType: p,
                dishes: obj[p]
            });
    }
    return res;
}

Call it with your input json and it will return the output.

kamilkp
  • 9,690
  • 6
  • 37
  • 56
0

You have to make an ad-hoc function/loop for that.
This is somewhat what you need to do:

// if you return a value in the function below, do this:
yourJson.dishList = reorganizeDishList(yourJson);

.

function reorganizeDishList(yourJson) {
    var dishList = yourJson.dishList;
    var tmpJson = {}; // this json will hold {type:[array of properties]}
    for (var i in dishList) { // loop ALL the dishes
        var properties = {}; // will hold all properties but type
        var type = '';
        for (var j in dishList[i]) { // loop All the properties of the dish
            if (j == 'dishType') {
                type = dishList[i][j];
            } else {
                properties[j] = dishList[i][j];
            }
        }
        if (!tmpJson[type]) {
            tmpJson[type] = []; // create array if tmpJson[type] is undefined
        }
        tmpJson[type].push(properties);
    }
    dishList = []; // clear old dishList
    for (var i in tmpJson) {
        // create newly organized array ("list")
        dishList.push({dishType: i, dishes: tmpJson[i]});
    }
    return dishList;
    // or `yourJson.dishList = dishList` instead of returning a value
}
Goodwine
  • 1,658
  • 1
  • 18
  • 31