Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Alberto Centelles
Alberto Centelles
8,356 Points

Foursquare API

Here is my code. The console says it has an "Uncaught SyntaxError: Unexpected token ILLEGAL " when it gets to "var script = '{".

I copy it here:

<html>
<body>

<h2>JSON Object Creation in JavaScript</h2>

<p id="demo"></p>

<script>
var script = '{
meta: {
code: 200
}
notifications: [
{
type: "notificationTray"
item: {
unreadCount: 8
}
}
]
response: {
categories: [
{
id: "4d4b7104d754a06370d81259"
name: "Arts & Entertainment"
pluralName: "Arts & Entertainment"
shortName: "Arts & Entertainment"
icon: {
prefix: "https://ss3.4sqi.net/img/categories_v2/arts_entertainment/default_"
suffix: ".png"
}
]
}
]
}
}';

var obj = JSON.parse(script);
console.log(obj);

</script>

</body>
</html>

Thank you very much

1 Answer

Aimee Ault
Aimee Ault
29,193 Points

A good way to debug these kinds of issues if you're not seeing the issue is to prune your JSON object down until you see where it's causing the issue. I think in this case, it's because your key names need to be strings, so "meta" instead of meta, etc.

shahardekel
shahardekel
20,306 Points

I agree with Aimee Ault . Try to validate your json object in an online JSON validator - jsonlint.com is a good choice to start with.