I am using AngularJS' http()
method to load in a JSON file located on my computer. The contents of the file are:
{
"topics": [
{
"canonical": "Nebraska Football",
"alias": "NEB",
"score": 45,
},
{
"canonical": "Ohio State Football",
"alias": "OSU",
"score": 10,
}
]
}
When I load the file (named example.json), I get an error message while using both Firefox and Safari that says Error: JSON.parse: expected double-quoted property name
. In Chrome (as expected thanks to its default security) it just plain doesn't work.
The JSON was originally loading fine in Firefox, but after a browser update it stopped working.
I am calling the JSON like this:
$http.get("json/example.json").success(function(data) {
console.log(data);
});
I tried using Python's SimpleHTTPServer to run the site as well, and it didn't make a difference. If anyone has any guesses as to what is going wrong here, I would really appreciate the help. Thanks!
Invalid Json try
{
"topics": [
{
"canonical": "Nebraska Football",
"alias": "NEB",
"score": 45
},
{
"canonical": "Ohio State Football",
"alias": "OSU",
"score": 10
}
]
}
If you run your JSON through jsonlint.com it points the error to },{
. If you look up from there you will see trailing ,
's that shouldn't be there. Your JSON should probably be:
{
"topics": [
{
"canonical": "Nebraska Football",
"alias": "NEB",
"score": 45
},
{
"canonical": "Ohio State Football",
"alias": "OSU",
"score": 10
}
]
}
Modified code: removing ',' from last item may solve your problem
{
"topics": [
{
"canonical": "Nebraska Football",
"alias": "NEB",
"score": 45
},
{
"canonical": "Ohio State Football",
"alias": "OSU",
"score": 10
}
]
}
Your problem is that you have extra ,
s in your JSON. Modify it as follows:
{
"topics": [
{
"canonical": "Nebraska Football",
"alias": "NEB",
"score": 45
},
{
"canonical": "Ohio State Football",
"alias": "OSU",
"score": 10
}
]
}
JSON Lint is a good place to validate your JSON. It's saying that you should remove the commas ,
after the final value in each array element. So "score":45,
should be "score":45