How to Pretty Print a Java Map

Java Map.toString() is convenient for debugging, but nested Maps and Lists quickly become difficult to read. Even a small record can fill a line:

{name=kim, age=30, address={city=seoul, zip=12345}, skills=[Java, Spring]}

This guide covers basic Map output, formatting entries yourself, using Jackson or Gson, and formatting an existing Map string copied from a log. The right approach depends on whether you still have the Java object and whether you need Map-style text or JSON.

Why Java Map output becomes hard to read

Common Map implementations represent entries inside braces, with equals signs between keys and values and commas between entries. Printing a Map calls its toString() method:

// map is an existing Map<String, Object>.
System.out.println(map);
{name=kim, age=30}

A nested Map adds another pair of braces; a List adds square brackets. Without indentation, it takes more effort to see where each structure ends. Larger collections make that single line harder to scan.

Using Map.toString()

For a quick inspection, ordinary printing is enough. This complete example uses LinkedHashMap so entries appear in insertion order:

import java.util.LinkedHashMap;
import java.util.Map;

public class MapOutput {
    public static void main(String[] args) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("name", "kim");
        map.put("age", 30);

        System.out.println(map.toString());
        // {name=kim, age=30}
    }
}

The standard implementation has no pretty-print option. HashMap does not guarantee entry order, so do not rely on its printed order. Custom Map classes can also override this representation. See the Java AbstractMap.toString() documentation for the standard format.

Pretty print by iterating over entries

If you only need one entry per line, iterate over entrySet(). Replace the print statement in the previous example with:

System.out.println("{");
for (Map.Entry<String, Object> entry : map.entrySet()) {
    System.out.println("    " + entry.getKey() + " = " + entry.getValue());
}
System.out.println("}");
{
    name = kim
    age = 30
}

This needs no external dependency and lets you choose the separators. It formats only the top level: nested Maps and Lists still use their own string representations. It is a simple display format, not JSON.

Pretty print a Map with Jackson

When you have the Map object and want JSON, Jackson can serialize it with indentation. This example uses Jackson 2.x and requires the com.fasterxml.jackson.core:jackson-databind dependency in your Java project; it is not part of the JDK.

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;

public class JacksonMapOutput {
    public static void printMap(Map<String, Object> map)
            throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper
            .writerWithDefaultPrettyPrinter()
            .writeValueAsString(map);

        System.out.println(json);
    }
}

Call JacksonMapOutput.printMap(map) with your Map and handle or declare the checked exception in the caller. For ordinary string-keyed Maps containing supported values, nested Maps become JSON objects and Lists become arrays. The result is JSON, not Java Map syntax. The ObjectMapper API documentation describes the pretty-print writer.

Pretty print a Map with Gson

Gson offers a similar option through GsonBuilder. Add the com.google.code.gson:gson dependency to your Java project before using this example:

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.Map;

public class GsonMapOutput {
    public static void printMap(Map<String, Object> map) {
        Gson gson = new GsonBuilder()
            .setPrettyPrinting()
            .create();

        System.out.println(gson.toJson(map));
    }
}

Call GsonMapOutput.printMap(map) with the original object. The output is formatted JSON with quoted string keys and values. Gson omits null object fields by default; use serializeNulls() on the builder if you need to retain them. See the Gson User Guide for configuration details.

What if you only have the Map.toString() output?

Sometimes all you have is a line copied from a log or console, such as {name=kim, age=30, address={city=seoul}}. You may not be able to rerun the Java code or access the original Map. Passing this text to a JSON serializer would serialize a string, rather than recover the Map structure.

For supported Map-style strings, use the Java Map Pretty Formatter. Paste the output, choose 2 or 4 spaces, and press Format. Processing runs locally in your browser. The formatter preserves Map syntax and treats scalar values as text.

Before

{name=kim, age=30, address={city=seoul, zip=12345}, skills=[Java, Spring]}

After (4 spaces)

{
    name=kim,
    age=30,
    address={
        city=seoul,
        zip=12345
    },
    skills=[
        Java,
        Spring
    ]
}

Java Map output vs JSON

Java Map-style text

{name=kim, active=true}

JSON

{
  "name": "kim",
  "active": true
}

Java Map.toString() uses equals signs; JSON uses colons. JSON object keys and string values require double quotes. Map output is therefore not necessarily valid JSON, and replacing equals signs with colons does not reliably convert it.

If you need JSON from supported Map-style text, use the Java Map to JSON Converter. It infers numbers, booleans, and null using its documented rules. A printed Map loses type information: for example, the string "true" and the boolean true can look the same. Serialize the original object when you need to preserve those distinctions.

Which method should you use?

Choosing a Map formatting approach
SituationRecommended approach
Have the actual Map objectJackson or Gson for JSON; custom Java formatting for another display format.
Need dependency-free simple outputIterate over entries and format each line yourself.
Only have Map.toString() outputJava Map Pretty Formatter for supported strings.
Need valid JSONJackson or Gson for objects; Java Map to JSON Converter for supported text.

Online formatter limitations

The online formatter supports nested Maps and Lists, including Maps inside Lists and Lists inside Maps. It cannot reconstruct every Java object from its string representation.

  • Commas inside quoted strings are not fully supported. For example, {name="Kim, Lee"} can produce an error because the comma is treated as an item separator.
  • DTO toString formats such as User(name=kim) are not supported.