Mark As Completed Discussion

Good morning! Here's our prompt for today.

In JavaScript by default, there is a method that can parse a given JSON string, and it can be invoked by calling JSON.parse(). The way this method works is constructing the JavaScript value or object described by the string.

Can you implement your own version of this method, called parseJSON, accepting one parameter (the JSON string), keeping in mind all the value types and restrictions in the JSON format?

JAVASCRIPT
1function parseJSON(input) {
2	// fill in this method
3}

Your task is to create a function called parseJSON that takes a string as input and returns the corresponding JavaScript object. The function should be able to handle JSON-like strings, which can be:

  • Primitive types: null, true, false
  • Strings wrapped in quotes: "string"
  • Arrays: [1, "a", null]
  • Objects: {"key": "value", "anotherKey": 42}

Your parser should return the corresponding JavaScript types for these string inputs. For example, if the input string is "null", your function should return null.

Question

Try to solve this here or in Interactive Mode.

How do I practice this challenge?

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Here's a video of us explaining the solution.

To change the speed of the video or see it in full screen, click the icons to the right of the progress bar.

Here's how we would solve this problem...

How do I use this guide?