JSON to javaScript array

json.parse array of objects
json multidimensional array
json nested arrays
json array length
get value from json object in javascript
json to array online
json to array php
json parse javascript

I'm having a problem handling JSON data within JavaScript, specifically in regards to using the data as an array and accessing and iterating through individual values. The JSON file is structured as follows:

  "head": {
    "vars": [ "place" , "lat" , "long" , "page" ]
  } ,
  "results": {
    "bindings": [
        "place": { "type": "literal" , "value": "Building A" } ,
        "lat": { "datatype": "http://www.w3.org/2001/XMLSchema#float" , "type": "typed-literal" , "value": "10.3456" } ,
        "long": { "datatype": "http://www.w3.org/2001/XMLSchema#float" , "type": "typed-literal" , "value": "-1.2345" } ,
        "page": { "type": "uri" , "value": "http://www.example.com/a.html" }
      } ,
        "place": { "type": "literal" , "value": "Building B" } ,
        "lat": { "datatype": "http://www.w3.org/2001/XMLSchema#float" , "type": "typed-literal" , "value": "11.3456" } ,
        "long": { "datatype": "http://www.w3.org/2001/XMLSchema#float" , "type": "typed-literal" , "value": "-2.2345" } ,
        "page": { "type": "uri" , "value": "http://www.example.com/b.html" }
      } ,
        "place": { "type": "literal" , "value": "Building C" } ,
        "lat": { "datatype": "http://www.w3.org/2001/XMLSchema#float" , "type": "typed-literal" , "value": "12.3456" } ,
        "long": { "datatype": "http://www.w3.org/2001/XMLSchema#float" , "type": "typed-literal" , "value": "-3.2345" } ,
        "page": { "type": "uri" , "value": "http://www.example.com/c.html" }

I want to be able to convert this into a JavaScript array as follows in order that I can iterate through it and pull out the values for each location in order:

var locations = [
        ['Building A',10.3456,-1.2345,'http://www.example.com/a.html'],
        ['Building B',11.3456,-2.2345,'http://www.example.com/b.html'],
        ['Building C',12.3456,-3.2345,'http://www.example.com/c.html']

Does anyone have any advice on how to achieve this? I have tried the following, but it picks up the "type" within the JSON, rather than just the value:

$.each(JSONObject.results.bindings, function(i, object) {
    $.each(object, function(property, object) {
        $.each(object, function(property, value) {

Any help, suggestions, advice or corrections would be greatly appreciated.

var locations = [];
$.each(JSONObject.results.bindings, function(i, obj) {
    locations.push([obj.place.value, obj.lat.value, obj.long.value, obj.page.value]);

Iterate through bindings, and put the properties place.value, lat.value, long.value and page.value from each element into an array, then add this array to locations.

Your current code uses object twice, as well as property, thus overwriting those variables. You should use unique variable names in nested loops to be able to distinguish between them.

JSON Arrays, var json_data = {"2013-01-21":1,"2013-01-22":7}; var result = []; for(var i in json_data) result.push([i, json_data [i]]); var data = new  When programming in JavaScript, the JavaScript engine would not interpret the string above as a JSON array but a string. If you want JavaScript engine to see that string as a array you would need to tell JavaScript to parse it into a JavaScript array by using JSON.parse(“[1,2,3,4,5]”). that would then return a JavaScript array as [1,2,3,4,5].

for a pure Javascript very similar to the accepted answer (which I like)

I like to use a negative while loop for speed (over a traditional for loop) when I have a defined length. This is likely faster than the jQuery answer also.

var i = JSONObject.results.bindings.length;
var locations = [];
while (i--) {
    t = JSONObject.results.bindings[i];
    locations.push([t.place.value, t.lat.value, t.long.value, t.page.value]);
//now show the places
var c = locations.length;
while (c--) {

Here is a fiddle to demonstrate: http://jsfiddle.net/MarkSchultheiss/JH7LR/

EDIT: Updated the example fiddle to stick the stuff in a div. (uses a little jQuery which was not part of the OP question so it is "added material" makes an assumption you have a <div id='holdem'></div> somewhere.

$(locations).each(function(i) {
    $('#holdem').prepend("<div>" + $(this)[0] + " Is at:" + this + "</div>");

For some fun I updated the fiddle to show the building name as a link to the building page: http://jsfiddle.net/MarkSchultheiss/JH7LR/3/

How to convert JSON object to JavaScript array?, It looks like a better fit for a key-value pair (which is essentially all that JavaScript objects are), than for an array. You can still access the data  Arrays in JSON are almost the same as arrays in JavaScript. In JSON, array values must be of type string, number, object, array, boolean or null. In JavaScript, array values can be all of the above, plus any other valid JavaScript expression, including functions, dates, and undefined.


for (i=0;i<loc.length;i++) {     
    for (prop in loc[i]) {

Where json is equal to the Json Object

[SOLVED] Convert JSON object to javascript array, This online tool helps you to convert JSON to JavaScript. Converting JSON to Normally, JSON object literal is not the same as JavaScript object literal. keys must be strings;; values can be strings, numbers, objects, arrays, true, false or null. JSON to JavaScript object. This online tool helps you to convert JSON to JavaScript. Converting JSON to JS is a common task when you use JSON samples in your JavaScript code. Normally, JSON object literal is not the same as JavaScript object literal.

you can do something like this

for (i=0;i<JSONObject.results.bindings.length;i++)

{newObject = JSONObject.results.bindings;
somethingelse = newObject[i].place.type;


JSON to JavaScript object literal, This array contains the values of JavaScript object obtained from the JSON string with the help of JavaScript. There are two approaches to solve this problem  1.JSON.stringify() and Object.assign() method convert array to JSON string. 2.JSON.parse() method convert string to JSON object in javascript.

You don't need jQuery for this.

var locations = [];
JSONObject.results.bindings.forEach(function(binding) {

How to convert JSON string to array of JSON objects using JavaScript, Pass PHP Arrays to JSON and JS with json_encode. The PHP json_encode function translates the data passed to it to a JSON string which can then be output to  It converts JavaScript simple or complex objects into a JSON string. It is also very much useful in converting a JavaScript array into a JSON string. Once the JavaScript object or array is converted into the string, then you have got the data in the form of JSON string, and we are ready to send this data to the server.

Pass PHP Arrays to JSON and JavaScript Using json_encode, An array begins with a left bracket [ and ends with a right bracket ] . Values are separated by a comma , . In JSON, property names or keys are always strings, while  When using the JSON.parse() on a JSON derived from an array, the method will return a JavaScript array, instead of a JavaScript object. Example The JSON returned from the server is an array:

JavaScript JSON Parsing, The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function  This new array contains index=0 as id: “1” and index=1 as name: “kiran” Create an Object with this values in map chain to return new array Next step deep copy the array of object to new object. Convert this object to JSON string using JSON.stringify() method. Here is an example code

JSON.parse(), Get code examples like "json to array javascript" instantly right from your google search results with the Grepper Chrome Extension. PHP provides a json_encode() function that converts PHP arrays into JavaScript. Technically, it is in JSON format. JSON stands for JavaScript Object Notation. Statement: If you have a PHP array and you need to convert it into the JavaScript array so there is a function provided by PHP that will easily convert that PHP array into the JavaScript array. But before using this function you need few things that first make sure you are using PHP version 5.2 or above.