Community

Start a Thread


Notifications
Subscribe You’re not receiving notifications from this thread.

Implement Json Stringify (Main Thread)

Here is the interview question prompt, presented for reference.

In Javascript there is a method called JSON.stringify() that can convert an object or a variable into a JSON string. The method handles different data types differently, ending up with its correct JSON "translation". Some examples of how this method handles different types include:

  • Boolean, Number, and String objects are converted to the corresponding primitive values
  • undefined, Function, and Symbol are not valid JSON values and are changed to null
  • The instances of Date are returned as date.toISOString() and are treated as strings
  • Infinity as well as the value null are considered as null

Can you implement a simplified version of this JSON stringifier, covering the most important constraints when it comes to the data type and value handling?

Here is an example of how your solution should work:

stringify({});                    // '{}'
stringify(true);                  // 'true'
stringify('foo');                 // '"foo"'
stringify([1, 'false', false]);   // '[1,"false",false]'
stringify([null, Infinity]);      // '[null,null]'
stringify({ x: 5 });              // '{"x":5}'

![cover](https://storage.googleapis.com/algodailyrandomassets/curriculum/frontend/interview-questions/implement-json-stringify.jpg)

You can see the full challenge with visuals at this link.

Challenges • Asked over 3 years ago by Jake from AlgoDaily

Jake from AlgoDaily Commented on Jun 04, 2022:

This is the main discussion thread generated for Implement Json Stringify (Main Thread).