-2

Can someone see what is wrong with this simple JSON parser? (JSFiddle Link)

I am getting an error in the console saying Uncaught SyntaxError: Unexpected token o

var a = document.getElementById('a');

var json = {
            type: "cow",
            sound: "moo",
            colors: ["black", "white", "brown"],
            feed: {
                types: ["hay", "grass"],
                consistency: "wet"
            }
        };

var parsed = JSON.parse(json);

//called with every property and it's value
function process(key,value) {
   console.log(key + " : " + value);
}

function traverse(o,func) {
    for (var i in o) {
        func.apply(this,[i,o[i]]);  
        if (o[i] !== null && typeof(o[i])=="object") {
            //going on step down in the object tree!!
            traverse(o[i],func);
        }
    }
}

//that's all... no magic, no bloated framework
traverse(json,process);
CodyBugstein
  • 21,984
  • 61
  • 207
  • 363

3 Answers3

1

You don't need to do any parsing whatsoever! Your data is already in a JavaScript object, not a JSON string.

This is often a confusion - JSON is a string, which represents data. JavaScript objects are the actual data stored by JS.

Scimonster
  • 32,893
  • 9
  • 77
  • 89
1

Your json isn't JSON, it's a reference to an object that was defined by the object initialiser on the right-hand side of that =. No JSON in sight. JSON is a textual, non-source-code notation for data exchange. There's no reason to parse what you have; the JavaScript engine already parsed it: http://jsfiddle.net/m36n6zoj/2/

The reason you get the error you get is that JSON.parse coerces its first argument to a string (since it's for parsing JSON, which is textual, and so held in strings in JavaScript code). If you take a generic object like the one your json refers to and turn it into a string, you get "[object Object]". So you're doing this:

JSON.parse("[object Object]")

To a JSON parser, that looks like the beginning of an array ([) followed by the letter o. The letter o is invalid in that position in JSON, and so you get the error.

Of course, if you really want to use JSON (perhaps you're normally going to load json via ajax or something and this is just test code), you can: http://jsfiddle.net/m36n6zoj/3/

var a = document.getElementById('a');

var json =
    '{' +
    '    "type": "cow",' +
    '    "sound": "moo",' +
    '    "colors": ["black", "white", "brown"],' +
    '    "feed": {' +
    '       "types": ["hay", "grass"],' +
    '       "consistency": "wet"' +
    '   }' +
    '}';

var parsed = JSON.parse(json);

//called with every property and it's value
function process(key,value) {
   console.log(key + " : " + value);
}

function traverse(o,func) {
    for (var i in o) {
        func.apply(this,[i,o[i]]);  
        if (o[i] !== null && typeof(o[i])=="object") {
            //going on step down in the object tree!!
            traverse(o[i],func);
        }
    }
}

//that's all... no magic, no bloated framework
traverse(parsed,process);
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

i think you have wrong json format , you need to add keys in double quotes like this

        "type": "cow"

this isnt a json object, it is a javascript object that you are providing so it wont be parsed, dou you want it to be JSON instead, go for stringify function than

A.B
  • 20,110
  • 3
  • 37
  • 71