Stringify a person object and show the text.
Acceptance criteria
- 1.#out should show {"name":"Ada"}.
JavaScript Tutorial · JavaScript Objects
These 10 tasks go with the JavaScript JSON chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “JSON is a text format for data, and JavaScript can convert objects to and from it.” Read the chapter.
1.Turn an object into textexercise · easy · 10 min
Stringify a person object and show the text.
Passes when: #out should show {"name":"Ada"}.
2.Turn text into an objectexercise · easy · 10 min · practice plan
Parse a JSON string and show the name from it.
Passes when: #out should show Ada.
3.Round trip an objectexercise · easy · 10 min · practice plan
Stringify then parse an object and show the year.
Passes when: #out should show 1815.
4.Make a deep copyexercise · easy · 10 min · practice plan
Copy a nested object through JSON, change the copy, and show the original city.
Passes when: #out should show London.
5.Handle invalid JSONexercise · medium · 10 min · practice plan
Wrap a parse in a try block and report when the text is not valid.
Passes when: #out should show invalid.
6.Fix parse used instead of stringifybug fix · easy · 8 min
An object needs turning into text, not the other way round.
Passes when: #out should show the object as JSON text.
7.Fix the text used without parsingbug fix · easy · 8 min · practice plan
A JSON string has no properties until it is parsed.
Passes when: #out should show Ada.
8.Fix the shallow copybug fix · easy · 8 min · practice plan
Spread shares the nested object, so the original was changed too.
Passes when: #out should show London.
9.Fix the unguarded parsebug fix · easy · 8 min · practice plan
Invalid text throws, so the parse needs a try block.
Passes when: #out should show invalid.
10.Fix the single quotes in the JSONbug fix · easy · 8 min · practice plan
JSON requires double quotes around every key and string.
Passes when: #out should show Ada.
Between them these tasks cover JSON.stringify, JSON.parse and Deep copying.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.