jsondecode.com logo

JSON in Java — Jackson, Gson & org.json

Java does not have built-in JSON support, but three libraries dominate: Jackson (most popular in enterprise), Gson (from Google, simple API), and org.json (lightweight, no dependencies).

Jackson — parse JSON into a POJO

Jackson's ObjectMapper is the standard choice for enterprise Java. Add com.fasterxml.jackson.core:jackson-databind to your build.

import com.fasterxml.jackson.databind.ObjectMapper;

public class User {
    public int id;
    public String name;
    public String email;
}

ObjectMapper mapper = new ObjectMapper();
String json = "{"id":1,"name":"Alice","email":"alice@example.com"}";
User user = mapper.readValue(json, User.class);
System.out.println(user.name); // Alice

Jackson — serialize to JSON

Use mapper.writeValueAsString() to serialize a POJO to a JSON string.

User user = new User();
user.id = 1;
user.name = "Alice";

String json = mapper.writeValueAsString(user);
// {"id":1,"name":"Alice","email":null}

// Pretty-print
String pretty = mapper
    .writerWithDefaultPrettyPrinter()
    .writeValueAsString(user);

Gson — parse and serialize

Gson is simpler for small projects. Add com.google.code.gson:gson to your build.

import com.google.gson.Gson;

Gson gson = new Gson();

// Parse
User user = gson.fromJson(json, User.class);

// Serialize
String output = gson.toJson(user);

Format and validate your JSON instantly

Free, no ads, no sign-up. Also converts JSON to TypeScript, YAML, CSV, and more.

Open JSON Formatter →