1

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?

3
  • Since you're using jquery, you should try $.parseJSON() api.jquery.com/jQuery.parseJSON Commented Nov 2, 2012 at 6:38
  • why the downvotes? jquery was one of the tags on the question :/ Commented Nov 2, 2012 at 7:24
  • I was downvoted as well, for my proposed solution, without any explanation. Other users were also downvoted. This is not fair. Commented Nov 3, 2012 at 7:24

5 Answers 5

2

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/

Sign up to request clarification or add additional context in comments.

Comments

1

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]);
});​

Comments

0

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!

1 Comment

I don't understand why somebody downvoted my answer here. This is a correct answer (anyone can test it in the console), I'm usign a well known javascript library, and it solves the problem the users asked about, showing research effort. It's completely unfair to downvote in this case.
-1

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)))​

2 Comments

Why the downvote?... I gave the OP TWO copy-paste solutions to the problem =0\
I was downvoted as well, without explanation! All good solutions, by the way.
-1

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.

1 Comment

Someone likes to downvote, without explanation or discussion. If a solution seems not appropriate, maybe leaving a comment could be more professional.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.