Abderrahman Msd Abderrahman Msd - 1 year ago 68
Javascript Question

Reading & Transforming JSON Object into Javascript Array

I want to transform a JSON object into an array.

Am using a PHP script to create a JSON file using this code:

$stores = array();
$reponse = $bdd->query('select * from store ');
while ($donnees = $reponse->fetch())
$stores[$i] = $donnees;


$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($stores));

In my JS file am using this code to load the data:

var json_obj = $.getJSON("results.json", function (data) {
json_obj = data;
return json_obj;


Now I want to transform my JSON object into a JavaScript array. But in specific way:

var props=[] ;


$.each(props, function (i,v)

Tim Tim
Answer Source

Looks like you can simply parse the json into your var props to create the array object...

var props = JSON.parse(json_obj);

props is now a js array object built from parsing your json string...

EDIT: proposed change to your js file

var props = [];
$.getJSON("results.json", function (data) {
    $.each( data, function(index) {


omit the rest of the code pushing to new array... just log out props...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download