Convert String to JSON in Java

JavaScript Object Notation (JSON) is an open standard file format and data interchange format, that uses human-readable text to store and transmit data objects consisting of key-value pairs and array data types.

To receive JSON String from a Java web service instead of XML.before processing the JSON data,
We need to convert it into a JSON object or Java Object. We can retrieve any field or set different values.

There are many open-source libraries that allow you to create JSON objects from JSON formatted String e.g. Gson, Jackson, and JSON-simple and org.json.

We see them one by one.

JSON String to Java object using JSON-Simple

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

we need dependency from maven as

<dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1.1</version> </dependency>

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

String to JSON - Jackson Example

Jackson is a popular JSON parsing library in Java. It's fast, feature-rich, and supports streaming which is useful for large JSON output from web services. In the following example we get a clear idea of how to use it.

Maven dependency <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.11.1</version> </dependency>
class Order { private String id; private String customerId; private double amount; private String status; //@Getter @Setter @toString }

public class JsontoObjectMapping { public static void main(String args[]) { String jsonStr = "{ \"id\" : \"10009\", \"customerId\" : \"sshfkdhd\",\"amount\" : \"5600.00\", \"status\" : \"confirmed\ }"; ObjectMapper mapper = new ObjectMapper(); Order order = mapper.readValue(jsonSource, Order.class); System.out.println(order); } } output: Order[ id = 10009 , customerId = sshfkdhd , amount = 5600.00 , status = confirmed]

The drawback of Jackson is that it requires JDK 1.5. If you want to use you must have JDK version equal or more than 1.5.

For more tutorial on Jackson please read the post  Jackson Object Mapper Examples

String to JSON Object using Gson

Gson is an open-source library for JSON in Java programs from google. To use Gson in our program we need to include it in our maven project.

<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.6</version> </dependency>

Code to do the processing of String JSON.

String jsonStr = "{ \"id\" : \"10009\", \"customerId\" : \"sshfkdhd\",\"amount\" : \"5600.00\", \"status\" : \"confirmed\ }"; Gson gson = new Gson(); Order order = gson.fromJson(jsonStr, Order.class)

Using org.json.JSONObject

The JSONObject used to convert String to a map-like object. The object provides methods for manipulating its contents, and for producing a JSON compliant object serialization. The JSONArray parse text from a String to produce the object.

We have maven dependencies

<dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20200518</version> </dependency>

We have an example

import org.json.JSONObject; import org.json.JSONArray; public class StringToJSON { public static void main(String args[]) { String jsonStr = "{ \"id\" : \"10009\", \"customerId\" : \"sshfkdhd\",\"amount\" : \"5600.00\", \"status\" : \"confirmed\ }"; JSONObject json = new JSONObject(str); String status = json.getString("status"); System.out.println(" JSON String is : "+ json.toString()); System.out.println("status is : "+status); } }

In this article, we have seen the  Convert String to JSON in Java with examples. All source code in the article can be found in the GitHub repository.