Talentd
FleetCode

2633. Convert Object to JSON String

Medium77.4% acceptance2 companiesPremium elsewhere, free here

Given a value, return a valid JSON string of that value. The value can be a string, number, array, object, boolean, or null. The returned string should not include extra spaces. The order of keys should be the same as the order returned by Object.keys().

Please solve it without using the built-in JSON.stringify method.

Example 1:

Input: object = {"y":1,"x":2}
Output: {"y":1,"x":2}
Explanation: 
Return the JSON representation.
Note that the order of keys should be the same as the order returned by Object.keys().

Example 2:

Input: object = {"a":"str","b":-12,"c":true,"d":null}
Output: {"a":"str","b":-12,"c":true,"d":null}
Explanation:
The primitives of JSON are strings, numbers, booleans, and null.

Example 3:

Input: object = {"key":{"a":1,"b":[{},null,"Hello"]}}
Output: {"key":{"a":1,"b":[{},null,"Hello"]}}
Explanation:
Objects and arrays can include other objects and arrays.

Example 4:

Input: object = true
Output: true
Explanation:
Primitive types are valid inputs.

Constraints:

  • value is a valid JSON value
  • 1 <= JSON.stringify(object).length <= 105
  • maxNestingLevel <= 1000
  • all strings contain only alphanumeric characters

Asked at 2 companies

Hints open one at a time

Hint 1
Consider the 4 possibilities. The object could be an array, an object, a string, or another type.
Hint 2
Think about the problem recursively. If you know how to convert any sub-data into a string, how could you use it to convert the entire data into a string?
Hint 3
If the data is a string, it's just the value surrounded by double quotes. If the data is another type, its just String(data). If the data is an array, it's the recursively stringified value of each item separated by commas. If the data is an object, it's a series of key-value pairs where each value is the recursively stringified value.
12:26
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
{"y":1,"x":2}
Input
{"a":"str","b":-12,"c":true,"d":null}
Input
{"key":{"a":1,"b":[{},null,"Hello"]}}
Input
true
Your code is saved on this device.Solution