Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript AJAX Basics (retiring) Programming AJAX Parsing JSON Data

Daniel Maldonado
Daniel Maldonado
18,756 Points

JSON.parse() gives error

So using JSON.parse() gives me an; "Uncaught SyntaxError: Unexpected token ; in JSON at position 142" but if I remove the JSON.parse() my data is returned to the console, no problem. So now I'm curious if my data is a string or not. If it's not, then why is JSON.parse() not working?

xmlRequest.onreadystatechange = function(){
  "use strict";
  if(xmlRequest.readyState === 4){
      var employees = xmlRequest.responseText;
      console.log(employees);
  }
};

Feel free to let me know what I'm missing. I'm on OS Chrome.

7 Answers

Tim Knight
Tim Knight
28,888 Points

Hi Daniel,

Using your code as the basis for mine this parsing came back with all of the objects in the JSON file as objects.

var xmlRequest = new XMLHttpRequest();
xmlRequest.onreadystatechange = function(){
  "use strict";
  if(xmlRequest.readyState === 4){
      var employees = JSON.parse(xmlRequest.responseText);
      console.log(employees);
  }
};
xmlRequest.open('GET', 'data/employees.json');
xmlRequest.send();
Daniel Maldonado
Daniel Maldonado
18,756 Points

Bummer.

I'm still getting an error so it's either Dw and/or Chrome that's giving me an issue? Not sure what else I can do at this point so I appreciate you double checking this for me.

Tim Knight
Tim Knight
28,888 Points

Daniel,

That code that I wrote was just in the Workspaces area using Chrome. You mentioned "Dw"β€”is that Dreamweaver? Are you doing this development locally on your computer? If you are, you'll want to make sure you're using some kind of local server to serve us this page because you can't make an AJAX call to a file that's using the file:// in the address to load, it'll have to be done over HTTP.

Windows or Mac, you might want to consider installing MAMP - https://www.mamp.info/. The free one is fine... but that'll let you run everything in a local server if you're not already doing that. There are some other solutions to do this which you might already be doing.

Daniel Maldonado
Daniel Maldonado
18,756 Points

Sorry for the confusion Tim. I am uploading everything to my server, but using Dreamweaver to code. Again not really sure why I'm getting errors.

I feel comfortable with jQuery ajax and getting .html pages to load asynchronously but I have decided to give up for now on this JSON. Way to difficult for no reason at this point.

Thanks for the help bud.

Tim Knight
Tim Knight
28,888 Points

No worries Daniel. If it's on a public server let me know if you want a second pair of eyes on it. I'd be happy to at least look at the public side of it.

Daniel Maldonado
Daniel Maldonado
18,756 Points

That's a great idea! Thanks so much man. here's the link http://www.danielmdesigns.com/test/ajax-fewd/employee_status.html

Then, of course, the error: Uncaught SyntaxError: Unexpected token ; in JSON at position 142

Let me know if you can spot where I'm messing up?

Tim Knight
Tim Knight
28,888 Points

Oh okay... so within your status.json just remove the semi-colon you have at the very bottom that closes out the array.

Daniel Maldonado
Daniel Maldonado
18,756 Points

wow. it returns an object.... JSON is so weird

thanks so much for your help bud