1

I have a json data, which need to convert in to separate key value pair to add in to a array.

what would be the correct way to make the json data into separate key pair data..?

I am looking for the best practice.

here is my json:

{
   "DashBoard":[

    {"title":"DashBoard"},
    {"widget":[{"slide":"To do"},{"slide":"Teamspace"},{"slide":"Recent Activity"}]},
    {"activity":[
        {"option":[{"show":"Volvo"},{"show":"Benz"},{"show":"Honda"}]},
        {"rows":[
            {"row" :  {"title":"Advert1", "text":"sample text1", "date":"22-06-2013"} }
            ,{"row" : {"title":"Advert2", "text":"sample text2", "date":"22-06-2014"} }
            ,{"row" : {"title":"Advert3", "text":"sample text3", "date":"22-06-2015"} }
            ,{"row" : {"title":"Advert4", "text":"sample text4", "date":"22-06-2016"} }
            ,{"row" : {"title":"Advert5", "text":"sample text5", "date":"22-06-2017"} } 
        ]}
    ]}

   ]
}

my output should be:

var arr = [

{"title":"DashBoard"},
{"slide":"To do"},
{"slide":"Teamspace"},
{"slide":"Recent Activity"},
{"show":"Volvo"},
{"show":"Benz"},
{"show":"Honda"}
{"row" :  {"title":"Advert1", "text":"sample text1", "date":"22-06-2013"},
{"row" : {"title":"Advert2", "text":"sample text2", "date":"22-06-2014"}
]

like so.

i try with :

newObj = {
                            "title"     : obj[0], 
                            "widget"    : obj[1]["widget"], 
                            "option"    : obj[2].activity[0]["option"], 
                            "rows"      : obj[2].activity[1]["rows"]
                        };


newObj = {
            "title"     : obj[0], 
            "widget"    : obj[1]["widget"], 
            "option"    : obj[2].activity[0]["option"], 
            "rows"      : obj[2].activity[1]["rows"]
        };



    $.each(newObj, function(key, value){

        if($.type(value) === "object"){
            newColl.push(value);
        }else if ($.type(value) === "array"){

            _.each(value, function(i,v){
                newColl.push(i);
            })

        }



    })

But not over come with best results. any one help me more correct way to get this? (basically i am converting this all in to models of backbone)

Henrik Andersson
  • 45,354
  • 16
  • 98
  • 92
3gwebtrain
  • 14,640
  • 25
  • 121
  • 247

1 Answers1

3

Challenge Accepted! Today I am in a good mood, so here you go:

function getArray(input) {
    var result = [];
    for (var i in input) {
        var val = input[i];
        if (Object.prototype.toString.call(val) === '[object Array]') {
            for(var j = 0; j < val.length; j++){
                result = result.concat(getArray(val[j]));
            }
        } else {
            var item = {};
            item[i] = val;
            result.push(item);
        }
    }
    return result;
}

var output = getArray(input);

Here is a working example

This assumes that input is your original object (not a JSON string). If you have a JSON string, the parse it first:

var input = JSON.parse(jsonString);
musefan
  • 47,875
  • 21
  • 135
  • 185