-3

I want to know what is the fastest function that can be used to convert a json object into a java script array here an example

var j = '[{"var1":"val1", "var2":"val2"}]';

var arr  ===> [var1] = "val1"

The bottom line is to avoid for loops as much as possible

Kijewski
  • 25,517
  • 12
  • 101
  • 143
Mohammad Abu Musa
  • 1,117
  • 2
  • 10
  • 32
  • 1
    You got a JSON string, not a JSON "object". – Kijewski Nov 09 '13 at 20:59
  • For future reference: In JavaScript there is no associative array. So using `someObject[key]=value;` the someObject be any Object `{}`. It can be an Array since Array is an Object and can have other properties assigned to it with `someArray[key]=value` but there is no need for it to be an Array. What you wanted to do is have Object A.key take the values of Object B.key where B is the first element in an array created with JSSON.parse You could do this with `var configObj={};...obj=JSON.parse(config)[0];for (var k in obj){ configObj[k]=obj[k];}` – HMR Nov 10 '13 at 04:49

3 Answers3

3

Most modern browsers will support the native JSON.parse function.

var arr = JSON.parse('[{"var1":"val1", "var2":"val2"}]');

console.log(arr);

//Just to be clear for OP
console.log(Array.isArray(arr)); //true

I want the output to be Arr[var1] = "val1"] not [Object]

That means you want to object at index 0 in the array.

var obj = JSON.parse('[{"var1":"val1", "var2":"val2"}]')[0];

console.log(obj['var1']); //val1

If you only want the values:

var values = JSON.parse('[{"var1":"val1", "var2":"val2"}]').reduce(function (values, obj) {
    for (var k in obj) values.push(obj[k]);
    return values;
}, []);

console.log(values); //["val1", "val2"]
plalx
  • 42,889
  • 6
  • 74
  • 90
  • Yes, every browser since IE8 knows `JSON.parse`: http://caniuse.com/json – Kijewski Nov 09 '13 at 21:01
  • I think the return of JSON.parse is Object not array, I want the return to be array type not Object – Mohammad Abu Musa Nov 09 '13 at 21:03
  • An Array is an Object in JavaScript. So the answer will work just fine. – Kijewski Nov 09 '13 at 21:05
  • That's because your input object is an array with a single item (ie object) in it. – Jondlm Nov 09 '13 at 21:06
  • I want the output to be Arr[var1] = "val1"] not [Object] var1: "val1" – Mohammad Abu Musa Nov 09 '13 at 21:07
  • @MohammadAbuMusa The return value is an `Array`, however `typeof arr` will be `object` if that's what you are looking at. – plalx Nov 09 '13 at 21:08
  • @MohammadAbuMusa I.e. you **don't** want the array but the contained object?? `var arr = JSON.parse(…)[0]`. – Kijewski Nov 09 '13 at 21:09
  • @kay exactly I want the contents of the object to merged into the array – Mohammad Abu Musa Nov 09 '13 at 21:11
  • @MohammadAbuMusa Have a look at the updated answer. – plalx Nov 09 '13 at 21:16
  • @MohammadAbuMusa you want the result to be an array (as in `arr.__proto__ === Array`), but it should have the keys of `arr[0]` to be merged into `arr`? – Kijewski Nov 09 '13 at 21:16
  • @plalx I think your solution can work, but it takes only values neglecting the keys. when I applied the code you provided it just neglected var1 as the key. I need both the key and the value I am trying to access them both. I have some trials to do – Mohammad Abu Musa Nov 09 '13 at 21:27
  • it just worked, thanks a lot I got now the key and the value and merged them into another array perfectly. do you have tips about its performance. I am still searching about some of the functions you used – Mohammad Abu Musa Nov 09 '13 at 21:34
  • This is the case I have been working on, I wanted to make TinyMCE works as EmberJS component so when I pass configurations through JSON TinyMCE is shown correctly. You can see a working example on http://jsbin.com/aRiPUkE/1/ – Mohammad Abu Musa Nov 09 '13 at 21:39
0

If I understand you correctly, you can use JSON.parse.

var json = '[{"var1": "val1", "var2": "val2"}]';
var arr = JSON.parse(json);
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
0

I usually use jQuery, though that might no longer be preferred.

var j = '[{"var1":"val1", "var2":"val2"}]';
var arr = jQuery.parseJSON( j );

This should work well in some older browsers if you need that kind of thing of course.

Rohit Gupta
  • 531
  • 7
  • 6