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 trialtom dennis
308 PointsParsing JSON when the JSON data array is unnamed
Hi, I followed the tutorial and everything worked just fine, however when I tried to experiment with a different JSON file which had unnamed array, I still had the same code as in the video, but adjusted line 3 (just changed the names) to the following below and everything else is untouched:
- response.on("end",function(){
- var profile = JSON.parse(body);
- printMessage(profile.title, profile.description, profile.date); 4.});
[ { "title": "title", "date": "2010", "description": "Lorem ipsum lorem ipsum" }, { "title": "title2", "date": "2012", "description": "Lorem ipsum lorem ipsum2" } ]
and when I run the code I get "undefined", could someone please help me to access these JSON values when the object is unnamed
this is the code for anyone who's not familiar
2 Answers
Vance Rivera
18,322 PointsI suggest you do a little debugging practice here and use the Developer Tools console provided by your web browser to figure out why your object is undefined. My bet is that your object or one of it's properties has no value therefore when you use the dot notation (ex: object.name) the result will always be undefined. So with the Developer tools if you go to your resources and select the js file with the code you are trying to test put a break point on the line where you are actually trying to print the values (start the action that runs your code) and inspect your object by hovering over 'profile'. You should be able to drill down into the object if it is defined with values if not then you found your culprit. Also another way to determine what your profile has is to log it to the console after your create the profile variable. So after JSON.parse(body) the next line should be console.log(profile);. This will print out the object to the javascript console in the Developer tools. FYI to start the developer tools press the F12 key on your open window with the website loaded. Hope this helps you find your way.
tom dennis
308 PointsNevermind, i fixed it by replacing printMessage with the following:
for (var myKey in profile){ printMessage(profile[myKey].title, profile[myKey].description,profile[myKey].date); }
tom dennis
308 Pointstom dennis
308 Pointsthe object is in the question and all the values are defined:
[ { "title": "title", "date": "2010", "description": "Lorem ipsum lorem ipsum" }, { "title": "title2", "date": "2012", "description": "Lorem ipsum lorem ipsum2" } ]