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 trialHanwen Zhang
20,084 Pointswhat is the difference between json() and json.parse()?
not a specific question for this chapter... but a question that confused me overall regarding JSON.
also, if we use URL for the data that we need for JSON, what the file .json for?
In the end, why I get so confused by the AJAX ASYNC section? ><
Thanks
1 Answer
Peter Vann
36,427 PointsHi Hanwen!
The difference is:
json() is asynchronous and returns a Promise object that resolves to a JavaScript object. JSON. parse() is synchronous can parse a string to (a) JavaScript object(s).
Info taken from here:
https://stackoverflow.com/questions/48295439/json-parse-vs-json
Keep in mind JSON.parse() is used to turn AJAX response JSON data (sent as a string) into a JSON object in the response handler code, so the values can be extracted efficiently/expediently from the JOSN (via the object attributes or key/value pairs).
More on JSON.parse():
https://www.w3schools.com/js/js_json_parse.asp
Also, be aware of JSON.stringify which does the opposite. It converts a JSON object to a JSON string.
More on JSON.stringify:
https://www.w3schools.com/js/js_json_stringify.asp
More on JSON():
https://developer.mozilla.org/en-US/docs/Web/API/Body/json
BTW,
JSON() is used with fetch, whereas JSON.parse is used with AJAX.
More on Fetch vs AJAX:
https://www.xul.fr/en/html5/fetch.php
I hope I didn't overload you with links, but it's not a very simple or trivial subject (kind of complicated and involved!?!)
I hope that helps.
Stay safe and happy coding!
Hanwen Zhang
20,084 PointsHanwen Zhang
20,084 PointsThank you for your information Peter