Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Actually I have an json object like this coming as an server response

{"0":"1","1":"2","2":"3","3":"4"}

SO I want to convert it into javascript array like

["1","2","3","4"]

Is there any good way to do this where ever I am reading people are using complex logic using loops. So Is there any other way to do this?

share|improve this question
    
duplicate : stackoverflow.com/questions/6857468/… –  Yenne Info Jan 2 '14 at 10:55

10 Answers 10

up vote 36 down vote accepted

Should be easy with jQuery

var arr = $.map(obj, function(el) { return el; });

FIDDLE

and almost as easy without jQuery

var arr = Object.keys(o).map(function(k) { return o[k] });

FIDDLE

assuming it's already parsed as an object, and isn't actually JSON

share|improve this answer
2  
fiddle is not working.. –  Nikhil Agrawal Jan 2 '14 at 11:02
    
@NikhilAgrawal - works just fine for me? –  adeneo Jan 2 '14 at 11:02
    
Works for me.. thank you :) –  Rafique Mohammed Aug 28 '14 at 6:23
    
@adeneo Sir can you please provide some explanation about htis methods. –  Nikhil Agrawal Nov 27 '14 at 7:53
var json = '{"0":"1","1":"2","2":"3","3":"4"}';

var parsed = JSON.parse(json);

var arr = [];

for(var x in parsed){
  arr.push(parsed[x]);
}

Hope this is what you're after!

share|improve this answer

There is nothing like a "JSON object" - JSON is a serialization notation.

If you want to transform your javascript object to a javascript array, either you write your own loop [which would not be that complex!], or you rely on underscore.js _.toArray() method:

var obj = {"0":"1","1":"2","2":"3","3":"4"};
var yourArray = _(obj).toArray();
share|improve this answer
    
Thanks - works like charm. But how to make it do the same for internal objects also (objects within objects)? Internal objects should also become flat members of the array at the root level (so that they can be passed to, say, datatables.net etc.) –  Gopalakrishna Palem Dec 18 '14 at 10:04

You simply do it like

var data = {
    "0": "1",
    "1": "2",
    "2": "3",
    "3": "4"
};
var arr = [];
for (var prop in data) {
    arr.push(data[prop]);
}
console.log(arr);

DEMO

share|improve this answer
var JsonObj= {"0":"1","1":"2","2":"3","3":"4"};
var array = [];
for(var i in JsonObj) {
    if(JsonObj.hasOwnProperty(i) && !isNaN(+i)) {
        array[+i] = JsonObj[i];
    }
}

DEMO

share|improve this answer

Nothing hard here. Loop over your object elements and assign them to the array

var obj = {"0":"1","1":"2","2":"3","3":"4"};
var arr = [];
for (elem in obj) {
   arr.push(obj[elem]);
}

http://jsfiddle.net/Qq2aM/

share|improve this answer

Try this:

var newArr = [];
$.each(JSONObject.results.bindings, function(i, obj) {
    newArr.push([obj.value]);
});
share|improve this answer
Try this...    

var obj = {"0":"1","1":"2","2":"3","3":"4"};
    var arr = [];
    arr = obj.toJSON();
share|improve this answer

Assuming your have a value like the following

var obj = {"0":"1","1":"2","2":"3","3":"4"};

Then you can turn this into a javascript array using the following

var arr = [];
json = JSON.stringify(eval('(' + obj + ')')); //convert to json string
arr = $.parseJSON(json); //convert to javascript array

This works for converting json into multi-diminsional javascript arrays as well.

None of the other methods on this page seemed to work completely for me when working with php json-encoded strings except the method I am mentioning herein.

share|improve this answer

Use this line:

var myArr = Array.prototype.slice.call(myObj, 0);

This uses the build in function in slice from the Array class. It is probably be the most effective one.

share|improve this answer
    
This doesn't seem to work: Array.prototype.slice.call({"0":"1","1":"2","2":"3","3":"4"}, 0); ==> [] –  Daniel Sokolowski Jun 20 '14 at 2:51

Your Answer

 
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.