Java has no built-in JSON parser
Unlike Python or JavaScript, the Java standard library ships nothing that converts a string to JSON. Every approach means adding a dependency, so the first decision is which one.
Jackson — the default choice
ObjectMapper mapper = new ObjectMapper();
// straight onto your own type
User user = mapper.readValue(json, User.class);
// or a generic tree when the shape is unknown
JsonNode node = mapper.readTree(json);
int id = node.get("id").asInt();
// to a Map, no wrapper class needed
Map<String, Object> map =
mapper.readValue(json, new TypeReference<Map<String, Object>>() {});
// object back to a JSON string
String out = mapper.writeValueAsString(user);Jackson is what Spring Boot autoconfigures, so in most projects it is already on the classpath.readValue maps onto a class, readTree gives you an untyped tree, and theTypeReference trick works around type erasure when the target is a generic collection.
Gson — smaller and simpler
Gson gson = new Gson();
User user = gson.fromJson(json, User.class);
String out = gson.toJson(user);org.json — quick, but dated
JSONObject obj = new JSONObject(json);
String name = obj.getString("name");
JSONArray items = obj.getJSONArray("items");
String out = obj.toString(2); // pretty-printedThe error everyone hits
org.json.JSONException: Value ... of type java.lang.String cannot be converted to JSONObject means the constructor received text that is not a JSON object. Three causes cover almost every case: the response was an HTML error page rather than JSON; the payload is a JSON array, so it needs new JSONArray(...); or the value is double-encoded — a quoted string that itself contains JSON.
// double-encoded: parse twice
String inner = new JSONObject("{\"body\":\"...\"}").getString("body");
JSONObject real = new JSONObject(inner);Paste the raw value into the converter above — it unwraps the nested layers and shows you the actual structure, which is usually faster than adding print statements.
A note on records
Since Java 16, records pair well with Jackson: declarerecord User(int id, String name) and readValue populates it directly, no getters or setters required. Add@JsonIgnoreProperties(ignoreUnknown = true) so an extra field in the response does not break deserialization — APIs add fields, and failing on unknown properties is rarely the behaviour you want in a client.
Reuse your ObjectMapper
Constructing a new ObjectMapper for every request is the most common performance mistake in Java JSON code. The class is expensive to build because it caches reflection metadata for every type it has seen, and that cache is exactly what makes the second parse of a type fast. It is thread-safe once configured, so create one instance, configure it, and share it — as a static field, or as a Spring bean you inject. If you need different settings for one call site, derive a ObjectReader or ObjectWriter from the shared mapper rather than building a second one.
Nulls, Optionals and dates
Jackson maps a missing field and an explicit null to the same thing — a null field — which loses a distinction that sometimes matters in PATCH-style APIs. Registering theJdk8Module lets you use Optional to tell them apart. Java 8 dates need the JavaTimeModule, without which LocalDateTime serializes as a verbose object of its component parts rather than an ISO string; adddisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) alongside it. Spring Boot registers both modules for you, which is why the same code often behaves differently inside and outside the framework — a discrepancy worth knowing about before you spend an afternoon on it.
Naming, briefly
Java convert string to JSON, java string to json,convert string to JSON object in Java, java object string to JSON,java to string to JSON, convert string to JSON java, andstring to JSON java all describe the same call — the library you picked decides its name. Going the other way, convert Java object to JSON string iswriteValueAsString in Jackson, toJson in Gson, andtoString() on a JSONObject. Theonline string to JSON converter is useful for verifying a raw payload before you wire it into your Java application.