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 trialKirt Perez
7,374 PointsIs .json() the same as JSON.parse()?
Title
1 Answer
Jennifer Nordell
Treehouse TeacherHi there, Kirt Perez! They are very similar, but not exactly the same. Yes, they both create JSON. However, the .json()
is asynchronous and returns a Promise
. While the JSON.parse is synchronous. The .json()
takes the Response
and turns it into JSON while the JSON.parse()
takes a JSON formatted string and returns any one of a few data types.
For more information, I recommend the following:
Hope this helps!
karan Badhwar
Web Development Techdegree Graduate 18,135 Pointskaran Badhwar
Web Development Techdegree Graduate 18,135 PointsHi, Jennifer Nordell, but on MDN it says, it returns a JavaScript Object not the JSON file. As, if it returns a JSON file, then how are we using it and extracting the JSON file to an Array of Objects to use. Moreover, @Guil also said it returns a JSON , but if it is returning the JSON, then where are we actually parsing the file into an array ?
MDN explanation below: -
Note that despite the method being named json(), the result is not JSON but is instead the result of taking JSON as input and parsing it to produce a JavaScript object.