3 ways to convert String to JSON object in Java? Examples (original) (raw)

It's very common nowadays to receive JSON String from a Java web service instead of XML, but unfortunately, JDK doesn't yet support conversion between JSON String to JSON object. Keeping JSON as String always is not a good option because you cannot operate on it easily, you need to convert it into a JSON object before you do anything else e.g. retrieve any field or set different values. Fortunately, there are many open-source libraries which allows you to create JSON object from JSON formatted String like Gson from Google, Jackson, and json-simple. In this tutorial, you will learn how to use these 3 main libraries to do this conversion with step-by-step examples.

Even though you can use a simple or complex JSON String with lots of attributes and JSON arrays, I'll use the following JSON String for example purpose:

jsonString = { "name" : "Ronaldo", "sport" : "soccer", "age" : 25, "id" : 121, "lastScores" : [ 2, 1, 3, 5, 0, 0, 1, 1 ] }

It's simple, has 5 attributes, two of which are String and the other two are numeric. One attribute, lastScore is a JSON array.

1. String to JSON Object using Gson

The Gson is an open-source library to deal with JSON in Java programs. It comes from none other than Google, which is also behind Guava, a common purpose library for Java programmers. You can convert JSON String to Java object in just 2 lines by using Gson as shown below :

Gson g = new Gson(); Player p = g.fromJson(jsonString, Player.class)

You can also convert a Java object to JSON by using the toJson() method as shown below

String str = g.toJson(p);

The good thing about Gson is that it's feature-rich and comes from Google, which is known for performance. If your JSON file is not too big then you can consider using Gson. Most of the config files which are in JSON can be easily parsed using Gson library.

2. JSON String to Java object using JSON-Simple

The JSON-Simple is another open-source library that supports JSON parsing and formatting. The good thing about this library is its small size, which is perfect for memory constraint environments like J2ME and Android.

JSONParser parser = new JSONParser(); JSONObject json = (JSONObject) parser.parse(stringToParse);

The good thing about json-simple is that it is also JDK 1.2 compatible, which means you can use it on a legacy project which is not yet in Java 5.

3 ways to convert String to JSON object in Java?

3. String to JSON - Jackson Example

Jackson is I guess the most popular JSON parsing library in the Java world. It's fast, feature-rich, and supports streaming which is great for parsing large JSON output from web services. Following one-liner convert JSON string representing a soccer player into a Java class representing player:

Player ronaldo = new ObjectMapper().readValue(jsonString, Player.class);

One of the drawbacks of Jackson is that it requires JDK 1.5 so if you are stuck in an earlier Java version then it may not fit there but I think that's highly unlikely in this era of Java 22. Most of the Java project I have worked on in recent years are either running on Java 11 or Java 17 and few are already started migrating to Java 21.

Also, Jackson doesn't support J2ME, but one of the main advantages of using Jackson is that it supports streaming which can be used to parse huge JSON responses without loading it fully in memory.

Jackson is a very popular and efficient Java library to map Java objects to JSON and vice-versa. If you want to learn the basics of the Jackson library and how to use them, I suggest you take a look at the Jackson documentation.

That's all about how to convert String to JSON objects in Java. You can use any of the json-simple, Gson, or Jackson for parsing JSON messages received from web services, each of them has its own advantage and disadvantages. For example, Json-simple has a small memory footprint means it's quite suitable for J2ME and Android clients where you have memory constraint while Jackson is feature-rich JSOn library which is probably better supported for a large project and application. Gson is in between them and also my favorite general-purpose JSON parser in Java programs.

Other JSON tutorials for Java programmers

Thanks for reading this article so far. If you like these 3 ways to convert String to JSON object in Java, then please share with your friends and colleagues. If you have any questions or feedback, then please drop a note.