0

I have data in the format

{
    "id1":["name1",10,20],
    "id2":["name2",12,20],
    "id3":["name3",14,21]
}

and am reading it in with

var dat;

$(document).ready(function() {
    $.getJSON('data.json', function(data) {
        dat = data.items;
        console.log(dat);
    });
});

This is proving problematic to read into properly because I gather (please clarify if I'm wrong) this is a poor json format as it lacks field names. Moreover the ':' seems to create something like a dictionary class and I'm not sure if that is preventing the resulting object from being iterable. If I call it in the console (e.g. dat[0]) I get "undefined", but perhaps there is an additional jquery error here.

So the question is how read this in so the data is accessible/iterable? Thanks in advance.

geotheory
  • 22,624
  • 29
  • 119
  • 196

4 Answers4

2

There is nothing wrong with the data format (after your edit), but you are trying to use a property items that doesn't exist.

You can loop through the items in the object, and access the values from each array:

$.each(data, function(key, value){
  // key contains "id1", "id2", and "id3" for the iterations
  // value is the array
  var name = value[0]; // contains "name1", "name2", "name3"
  var num1 = value[1]; // contains 10, 12, 14
  var num2 = value[2]; // contains 20, 20, 21
});

Note: The order of items in an object is undefined, so you can get them in the order "id1", "id2", "id3" or any other order like "id2", "id3", "id1". Different browsers will return them in different order.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • This is spot on, thanks. Slightly streamlined version with usable data object output: `dat = []; $.each(data, function(key, value) dat[dat.length] = {name:value[0], num1:value[1], num2:value[2]});` – geotheory Jul 22 '14 at 10:09
  • 1
    @geotheory: Even more streamlined with the `map` method: `var dat = $.map(data, function(value, key) { return { name:value[0], num1:value[1], num2:value[2] }; });`. :) – Guffa Jul 22 '14 at 11:09
0

When JQuery calls the call-back the data parameter is the object that results from parsing the JSON. It doesn't have an items attribute. I'm not sure what you're trying to do, but you could do this:

$(document).ready(function() {
    $.getJSON('data.json', function(data) {
        $.each( data, function( key, val ) {
            console.log("Key: " + key + "; value: " + val);
        });
    });
});
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

I believe you are confused as to what data structure JSON follows.

JSON does follow a similar structure to a Dictionary. In JavaScript though, there is no .items() method (I assume that's what you meant with data.items) like there is in other languages like Python. When you write data.property it is equivalent to data['property'], just like getting the value of a key in a dictionary data structure.

On how to parse objects, take a look at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#Enumerating_all_properties_of_an_object

You can find some descriptions of parsing JSON here as well How to get all properties values of a Javascript Object (without knowing the keys)? if the other answers don't quite work for your case.

Community
  • 1
  • 1
agilgur5
  • 667
  • 12
  • 30
  • I meant Object Literal, but yes there are some minute differences between the two regardless -- I edited that out as that can be confusing, thanks! – agilgur5 Jul 21 '14 at 17:55
  • Yes, JSON and object literal notation are similar but not the same. Certainly JSON and object literals themselves are basically unrelated, and JSON and Javascript objects are completely unconnected. – Lightness Races in Orbit Jul 21 '14 at 18:13
0

http://jsfiddle.net/EHret/

few different ways to reach the data.

var data =  {
    "id1":["name1",10,20],
    "id2":["name2",12,20],
    "id3":["name3",14,21]
};

$("div").append(data["id1"] + "<br />");
$("div").append(data["id2"][0] + "<br />");
$("div").append(data["id2"][1] + "<br />");
$("div").append(data["id2"][2] + "<br />");

$.each(data, function(index, val){
    $("div").append(index + " : " + val + "<br />");
    $.each(val, function(key, value){
        $("div").append(key + " : " + value + "<br />");
    });
});
eg_dac
  • 701
  • 4
  • 14