So I simply want to get 1 value stored in an external JSON file in the same folder. Every time I try to get the item, chrome says "unexpected token : " also, it shows another error saying that data in the JSON.parse(data) method is wrong. Either way, I'm getting that token error with or without the parse(). it doesn't like the "e1" :
part, there is a red error line under that portion of the json file. I've seen about 6 suggestions on this site and others but it didn't help my particular issue. Any thoughts?
here is my code:
<!DOCTYPE html>
<html>
<head>
<title>Objects 2</title>
<script type="text/javascript" src="data.json"></script>
</head>
<body>
<div id="test"></div>
<script>
var extJSON = JSON.parse(data);
document.getElementByID("test").innerHTML = extJSON.e1.position;
</script>
</body>
</html>
And here is my JSON file
{
"e1" :
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 42,
"position" : "mechanic",
"phone" :
{
"home" : "718-111-1111",
"mobile" : "718-222-2222",
"work" : "718-333-3333"
}
},
"e2" :
{
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 34,
"position" : "Investor",
"phone" :
{
"home" : "305-111-1111",
"mobile" : "305-222-2222",
"work" : "305-333-3333"
}
},
"e3" :
{
"first_name" : "Rusty",
"last_name" : "Colon",
"age" : 23,
"position" : "Diver",
"phone" :
{
"home" : "415-111-1111",
"mobile" : "415-222-2222",
"work" : "415-333-3333"
}
},
"e4" :
{
"first_name" : "Anna",
"last_name" : "Spencer",
"age" : 20,
"position" : "Event Planner",
"phone" :
{
"home" : "786-111-1111",
"mobile" : "786-222-2222",
"work" : "786-333-3333"
}
}
}
I simply want to print any value or property value pair. Thanks.