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?

share|improve this question

75% accept rate
Since you're using jquery, you should try $.parseJSON() api.jquery.com/jQuery.parseJSON – Mike Trusov 11 hours ago
why the downvotes? jquery was one of the tags on the question :/ – Mike Trusov 10 hours ago
But it doesn't answer the question... or work in this case. He's already got an array of objects, not a JSON string (his wording and his example code are conflicting) – BLSully 3 hours ago
feedback

5 Answers

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]);
});​
share|improve this answer
What is .value? – Ian 11 hours ago
@Ian - thanks to pointing that you tis not needed... – Pranay Rana 10 hours ago
feedback

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/

share|improve this answer
feedback

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!

share|improve this answer
feedback

The proposed solution is not general-purpose, and it can't be reused without modifying it (e.g. if the JSON array has different key names, or if it's nested).

I would suggest:

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

   var jsArray = JSON.parse( jsonString );

Reference: http://www.json.org/js.html.

share|improve this answer
feedback

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)))​
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.