Convert JSON to Map
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Converting JSON to a map sounds simple, but there are really two steps hiding inside that sentence. First, you parse JSON text into a native data structure. Then, if you specifically want a map type, you convert the resulting object into that map. In JavaScript, that usually means JSON.parse followed by new Map(Object.entries(...)).
Parse the JSON Text First
JSON is a string format. A Map is a runtime data structure. So the first step is always parsing.
After JSON.parse, you have a normal JavaScript object, not a Map.
That distinction matters because many developers assume parsing JSON automatically creates a Map. It does not.
Convert the Parsed Object to a Map
If the JSON represents a flat object, conversion is easy:
Object.entries(obj) turns the object into an array of key-value pairs, and Map accepts exactly that format.
This is the simplest correct answer for JavaScript.
Nested Objects Need Recursive Conversion
If the JSON contains nested objects, only the top level becomes a Map unless you convert inner objects too.
Without this extra step, nested objects stay as plain objects.
Know When a Plain Object Is Better
In JavaScript, a parsed JSON object is often already the right shape. You do not always need to convert it to Map.
Use a plain object when:
- the data came from JSON and will go back to JSON
- simple property access is enough
- you want the most familiar shape for API responses
Use a Map when:
- you want explicit map semantics
- keys may be added and removed frequently
- you want methods such as
.get(),.set(), and.has()
If the data is just application config loaded once from JSON, a plain object is usually simpler.
If You Meant Java, Use a JSON Library
Some people asking this question actually mean Java’s Map<String, Object>. In that case, use a JSON library such as Jackson.
So the correct conversion method depends on which language’s Map you actually mean.
Common Pitfalls
The biggest mistake is trying to call new Map(jsonString) directly. Map expects iterable key-value pairs, not raw JSON text.
Another common issue is forgetting that JSON.parse returns a plain object in JavaScript, not a Map.
Nested data is another trap. new Map(Object.entries(obj)) converts only one level unless you recurse manually.
Finally, note that JSON.stringify does not serialize a Map the way many people expect. If you need to turn a Map back into JSON, you usually convert it back to an object or array first.
Summary
- Parse JSON text first with
JSON.parse. - In JavaScript, convert a flat parsed object with
new Map(Object.entries(obj)). - Use recursive conversion if nested objects should also become maps.
- In many JavaScript cases, a plain object is simpler than a
Map. - If you meant Java’s
Map, use a JSON library such as Jackson to parse directly into it.
Related reading
- Convert MySql DateTime stamp into JavaScript's Date format
- Converting JS object to ArrayBuffer to transfer to/from web worker equals bottleneck
- Copy array items into another array
- Copy to clipboard using Javascript in iOS
- Correct usage of ENV in distributed Node system
- Could not auto-determine entry point from rollupOptions
- Could not find a declaration file for module 'module-name'. '/path/to/module-name.js' implicitly has an 'any' type
- Could not load credentials from any providers while using dynamodb locally in Node
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.