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 trialSebastian Hewelt
Front End Web Development Techdegree Student 1,254 PointsDon't we need both key and value fo the proper JSON file?
Hey,
I wonder why is this a correct JSON.
[ "AJAX Basics" , "jQuery Basics" ]
Is it correct but useless? Are there any circumstances in which it might be worthwhile?
2 Answers
Aaron Martone
3,290 PointsIn short, since arrays have numeric indexes (they can support key names, though it's a antipattern), the data structure can be presented using the array syntax. It's still valid JSON. It still resolves to an array of values, there just will never be any key/value pairs under it.
Here, the data returned is decoded into a JS object, an array. Its keys are the numeric indices, its values that which is returned by the index.
For JSON the returns an object literal, it too is decoded into a JS object, as an object, and its keys are the expected keys with their values their respective values. Most of the time, JSON data holds such in-depth data, that an object is just more common to return. But it's not the only supported return format.
Seth Kroger
56,413 PointsIt's correct because it's just an array (denoted by the []'s) of plain strings. An object which which does need key, value pairs is surrounded by {}'s. Same as a JavaScript object literal.