JSON
Data Conversion
JavaScript
Maps
Programming

Convert JSON to Map

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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.

javascript
1const json = '{"name":"Alice","age":30,"city":"Toronto"}';
2const obj = JSON.parse(json);
3
4console.log(obj.name); // Alice

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:

javascript
1const json = '{"name":"Alice","age":30,"city":"Toronto"}';
2const obj = JSON.parse(json);
3const map = new Map(Object.entries(obj));
4
5console.log(map.get("name")); // Alice
6console.log(map.get("age"));  // 30

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.

javascript
1function objectToMap(value) {
2  if (Array.isArray(value)) {
3    return value.map(objectToMap);
4  }
5
6  if (value !== null && typeof value === "object") {
7    return new Map(
8      Object.entries(value).map(([key, innerValue]) => [key, objectToMap(innerValue)])
9    );
10  }
11
12  return value;
13}
14
15const json = '{"user":{"name":"Alice","roles":["admin","editor"]}}';
16const parsed = JSON.parse(json);
17const deepMap = objectToMap(parsed);
18
19console.log(deepMap.get("user").get("name")); // Alice

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.

java
1import com.fasterxml.jackson.databind.ObjectMapper;
2import java.util.Map;
3
4public class JsonToMapExample {
5    public static void main(String[] args) throws Exception {
6        String json = "{\"name\":\"Alice\",\"age\":30}";
7        ObjectMapper mapper = new ObjectMapper();
8
9        Map<String, Object> map = mapper.readValue(json, Map.class);
10        System.out.println(map.get("name"));
11    }
12}

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.

Course illustration
Course illustration

All Rights Reserved.