Convert Map to JSON String
Difficulty: Easy · Topics: JSON, Serialization, Map · Acceptance: 100.0% of 6 submissions
Difficulty: Easy | Tags: JSON, Serialization, Map
Description
Given a Map where values can be strings, numbers, booleans, nested maps, or lists, convert it into a valid JSON object string.
Implement:
convertMapAsJSONObjectString(map) → String
Examples
Example 1:
Input: {"name": "Alice", "age": 30}
Output: "{\"name\":\"Alice\",\"age\":30}"
Example 2 — Nested:
Input: {"user": {"name": "Bob", "scores": [90, 85, 77]}}
Output: "{\"user\":{\"name\":\"Bob\",\"scores\":[90,85,77]}}"
Example 3 — Empty map:
Input: {}
Output: "{}"
Constraints
- Keys are always non-null strings.
- Values can be:
String,Number,Boolean,null,Map, orList. - Nesting depth ≤ 10.
- No circular references.
Hints
Hint 1
Recursively handle Map and List types; base types are directly serializable.
Follow-up
- Handle special characters in strings (newlines, tabs, quotes) with proper escaping.