Working with JSON in Java

Java doesn't have built-in JSON support, but there are several excellent libraries available. This guide covers the three most popular: Jackson, Gson, and org.json.

Using Jackson (Recommended)

Jackson is the most popular and feature-rich JSON library for Java.

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JacksonExample {
    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();

        // Parse JSON string to object
        String json = "{\"name\": \"John\", \"age\": 30}";
        User user = mapper.readValue(json, User.class);
        System.out.println(user.getName()); // John

        // Convert object to JSON
        User newUser = new User("Jane", 25);
        String jsonOutput = mapper.writeValueAsString(newUser);
        System.out.println(jsonOutput);

        // Pretty print
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        String prettyJson = mapper.writeValueAsString(newUser);
        System.out.println(prettyJson);
    }
}

Using Gson

Gson is Google's JSON library, known for its simplicity.

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class GsonExample {
    public static void main(String[] args) {
        Gson gson = new Gson();

        // Parse JSON string
        String json = "{\"name\": \"John\", \"age\": 30}";
        User user = gson.fromJson(json, User.class);
        System.out.println(user.getName());

        // Convert to JSON
        User newUser = new User("Jane", 25);
        String jsonOutput = gson.toJson(newUser);

        // Pretty print
        Gson prettyGson = new GsonBuilder()
            .setPrettyPrinting()
            .create();
        String prettyJson = prettyGson.toJson(newUser);
        System.out.println(prettyJson);
    }
}

Using org.json

org.json is a simple, lightweight JSON library.

import org.json.JSONObject;
import org.json.JSONArray;

public class OrgJsonExample {
    public static void main(String[] args) {
        // Parse JSON string
        String json = "{\"name\": \"John\", \"age\": 30}";
        JSONObject obj = new JSONObject(json);

        String name = obj.getString("name");
        int age = obj.getInt("age");

        // Create JSON
        JSONObject newObj = new JSONObject();
        newObj.put("name", "Jane");
        newObj.put("age", 25);

        // Create JSON array
        JSONArray hobbies = new JSONArray();
        hobbies.put("reading");
        hobbies.put("coding");
        newObj.put("hobbies", hobbies);

        // Pretty print
        String prettyJson = newObj.toString(2);
        System.out.println(prettyJson);
    }
}

JSON Validation

import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonValidator {
    private static final ObjectMapper mapper = new ObjectMapper();

    public static boolean isValidJson(String json) {
        try {
            mapper.readTree(json);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}

Best Practices