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

Hints

Hint 1

Recursively handle Map and List types; base types are directly serializable.

Follow-up

Practice this problem

Related problems