V.V
V.V

Reputation: 883

How to convert json to java script array?

I have the json string like,

string js=[{"Name":"Pini","ID":"111"},

{"Name":"Yaniv","ID":"123"},

{"Name":"Yoni","ID":"145"}]

And I need to convert this into like the following format using java script.

[['Pini',111],['Yaniv',123],['Yoni',145]]

How to convert the json string into javascript array using javascript function?

Upvotes: 1

Views: 592

Answers (5)

Alberto Arena
Alberto Arena

Reputation: 343

I correct my solution, I hadn't read well the specs (my fault):

var jsonArray = [{"Name":"Pini","ID":"111"}, {"Name":"Yaniv","ID":"123"}, {"Name":"Yoni","ID":"145"}];

var jsonConverted = {};
$(jsonArray).each( function() {
    jsonConverted.push([ this.Name, this.ID ]);
});

This solution uses jQuery.

Upvotes: -1

For this kind of transformations I always prefer using UnderscoreJs which is an utility-belt library (mainly for object/array transformations) for Javascript. I think that it provides great functions that make life easier, allowing javascript developers to be more productive and to achieve a better legibility for the resulting code.

Here is the solution with underscore (extended):

var js=[{"Name":"Pini","ID":"111"},  
        {"Name":"Yaniv","ID":"123"},    
        {"Name":"Yoni","ID":"145"}]

var names = _.pluck(js, 'Name');
var ids = _.pluck(js, 'ID');

var result = _.zip(names, ids)

And you achive the desired result:

[['Pini',111],['Yaniv',123],['Yoni',145]]

Solution in one line with underscorejs:

var result = _.zip(_.pluck(js, 'Name'), _.pluck(js, 'ID'))

Hope it helps!

Upvotes: 0

BLSully
BLSully

Reputation: 5939

Here's a solution that will work for a simple object (not deep objects, though.... but I'll leave that for you to implement).

http://jsfiddle.net/eUtkC/

var js = [{
    "Name": "Pini",
    "ID": "111"},
{
    "Name": "Yaniv",
    "ID": "123"},
{
    "Name": "Yoni",
    "ID": "145"}]

function obj2arr(obj) {
    var i, key, length = obj.length,
        aOutput = [],
        aObjValues;
    for (i = length - 1; i >= 0; i--) {
        aObjValues = [];
        for (key in obj[i]) {
            if (obj[i].hasOwnProperty(key)) {
                aObjValues.push(obj[i][key]);
            }
        }
        aOutput.push(aObjValues);
    }
    return aOutput;
}

document.write(JSON.stringify(obj2arr(js)))​

EDIT

Here's a version using Array.prototype.map:

http://jsfiddle.net/eUtkC/1/

function obj2arr(obj) {
    var key, aOutput = [];
    for (key in obj) {
        if (obj.hasOwnProperty(key)) {
            aOutput.push(obj[key]);
        }
    }
    return aOutput;
}

document.write(JSON.stringify(js.map(obj2arr)))​

Upvotes: -1

Pranay Rana
Pranay Rana

Reputation: 176956

You can do like this

JsFiddler Demo of below code

var JSONObject = {"results":[{"Name":"Pini","ID":"111"},
                  {"Name":"Yaniv","ID":"123"},
                             {"Name":"Yoni","ID":"145"}]};


var Users= [];

$.each(JSONObject.results, function(i, obj)
       {
    alert(obj.Name);
    alert(obj.ID);
    Users.push([obj.Name, obj.ID]);
});​

Upvotes: 1

Ian
Ian

Reputation: 50933

I think something like this:

var ret = [];

for (var i = 0; i < js.length; i++) {
    ret.push([js[i].Name, js[i].ID]);
}

// ret holds your array of arrays

Or something like:

var ret = $.map(js, function (el, i) {
    return [[el.Name, el.ID]];
});

An example of both: http://jsfiddle.net/RNY8M/

Upvotes: 2

Related Questions