Jez D
Jez D

Reputation: 1489

How do I convert a multi dimensional JSON object into javascript array

I have a JSON object which looks like this:

[{"tabname":"orders","datagroups":[{"dataname":"ordersToday","datavalue":9},{"dataname":"orders30Days","datavalue":126}]}] 

When I use console.log($.parseJSON(thedata)) I just get the word Object and no actual data.

How do I organise this data into a multidimensional javascript array? so that it looks something like this:

array("tabname"=>"orders", "datagroup"=>array(array("dataname"=>"ordersToday", "datavalue"=>9),array("dataname"=>"orders30Days","datavalue"=>126)))  

Upvotes: 1

Views: 4619

Answers (3)

Jez D
Jez D

Reputation: 1489

Thanks to everyone for contributing. I took a break then came back and figured it out. The way my brain works is all wrong.

To access the individual values, I needed to do something like this:

var orderStats = $.parseJSON(data);
console.log(orderStats[0].datagroups[0].dataname);

Upvotes: 0

RobinJ
RobinJ

Reputation: 5243

It's quite simple, really.
You can simply use jQuery's $.parseJSON (jsonString).

Upvotes: 0

Šime Vidas
Šime Vidas

Reputation: 185883

It is an array:

var json = '[{"tabname":"orders","datagroups":[{"dataname":"ordersToday","datavalue":9},{"dataname":"orders30Days","datavalue":126}]}]';

var obj = $.parseJSON(json);

Array.isArray(obj) // => true

Upvotes: 1

Related Questions