H
H
habrazhitel2014-11-25 21:26:30
JavaScript
habrazhitel, 2014-11-25 21:26:30

How to convert json to object?

There is such a json (of course, there can be more data in data):

{ "cols": [ "Author", "Album", "Date", "Price", "ID" ], "data": [ [ "Eminem", "Marshall Mathers", "2000", "10", "id1"], [ "Eminem", "Relapse", "2009", "10", "id2"] ] }

and so, it is possible to transform it somehow into such object?
{ "Author": "Eminem",
"Album": "Marshall Mathers",
"Date": "2000",
"Price": "10",
"ID": "id1"},
{ "Author": "Eminem",
"Album": "Relapse",
"Date": "2009",
"Price": "10",
"ID": "id2"}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2014-11-25
@habrazhitel

JSON.stringify and JSON.parse
update:

var keys = response.cols;

data = response.data.map(function (item) {
    return item.reduce(function (result, value, idx) {
        result[keys[idx]] = value;
        
        return result;
    }, {});
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question