Convert Object to JSON String without `JSON.stringify`
Medium
24
2
25.3% Acceptance
In this lab, you have to implement a function called jsonStringify
that converts an object to a valid JSON string without using JSON.stringify
built-in method. You can assume the input object only includes strings, integers, arrays, objects, booleans, and null. The returned string should not include extra spaces. The order of keys should be the same as the order returned by Object.keys()
.
This is a fun exercise to understand how JSON stringification works and what intricacies are involved while converting objects to JSON strings.
Examples:
jsonStringify({"y": 1, "x": 2}) // Output: '{"y":1,"x":2}' jsonStringify({"a":"str","b":-12,"c":true,"d":null}) // Output: '{"a":"str","b":-12,"c":true,"d":null}' jsonStringify({"key":{"a":1,"b":[{},null,"Hello"]}}) // Output: '{"key":{"a":1,"b":[{},null,"Hello"]}}'
Important Notes:
- Write the function using ESM import/export syntax.
- The evaluation script is extremely important; make sure to write it without errors or missing parts.
- When writing the evaluation script, keep these points in mind:
- The final length of the
testlog
array should be the same as the number of challenges. - The order of try-catch blocks in the evaluation script must match the order of written challenges.
- Import any user code dynamically inside every challenge to avoid lab crashing if the file(s) do not exist.
- The final length of the
- Add any required packages to dependencies or devDependencies in your package.json file.
- For the lab files, keep .cdmrc file exactly the same as provided above.