Convert Java into JSON and JSON into Java. All Possible Examples.

Learning how to convert Java objects into JSON and JSON into Java will help you a lot in your work as a full stack mobile app developer. In this blog post I will share with you code examples which will help you do most of the tasks you will even need to do when working with JSON and Java. I will share with you:

  • How to create a JSON object in Java
  • How to create JSON object from a JSON String
  • How to convert Java JSON object into a JSON String
  • How to convert Java Object (POJO) into a JSON Object and a JSON String
  • Convert JSON Array String in Java Array List
  • How to convert Java objects like Map or List into JSON objects

I think the above is all you will most often be coming across with and the below code examples are all you need to help you with your task.

Before you begin, you will need to either download Google GSON library or add the below dependency into your pom.xml. At the time I am writing this blog post the GSON library is of version 2.8.0.

GSON Dependency for pom.xml

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.0</version>
</dependency>

 Create a JSON object in Java

// Create new JSON Object
JsonObject person = new JsonObject();
person.addProperty("firstName", "Sergey");
person.addProperty("lastName", "Kargopolov");

System.out.println(person.toString());

 Create Inner/Nested JSON Object

JsonObject person = new JsonObject();
person.addProperty("firstName", "Sergey");
person.addProperty("lastName", "Kargopolov");

// Create Inner JSON Object 
JsonObject address = new JsonObject();
address.addProperty("country", "ru");
address.addProperty("city", "Moscow");

person.add("address", address);

System.out.println(person.toString());

 Create JSON Array

// Create JSON Array 
JsonArray languages = new JsonArray();
languages.add("Russian");
languages.add("English");
languages.add("French");

System.out.println(languages.toString());

 Create JSON Array from String

// Create JSON Array from String
String jsonString = "{\"firstName\":\"Sergey\",\"lastName\":\"Kargopolov\"}";
JsonParser jsonParser = new JsonParser();
JsonObject objectFromString = jsonParser.parse(jsonString).getAsJsonObject();
 
System.out.println(objectFromString.toString());

Convert JSON Array String into JSON Array

JsonParser jsonParser = new JsonParser();

// Convert JSON Array String into JSON Array 
String jsonArrayString = "[\"Russian\",\"English\",\"French\"]";
JsonArray arrayFromString = jsonParser.parse(jsonArrayString).getAsJsonArray();

System.out.println(arrayFromString.toString())

Convert JSON Array String into Java Array List

// Convert JSON Array String into Java Array List
 String jsonArrayString = "[\"Russian\",\"English\",\"French\"]";
 Gson googleJson = new Gson();
 ArrayList javaArrayListFromGSON = googleJson.fromJson(arrayFromString, ArrayList.class);
 
 System.out.println(javaArrayListFromGSON);

Convert Java Plain Object into JSON

//Convert Java Plain Object into JSON
Person personPojo = new Person();
personPojo.setFirstName("Sergey");
personPojo.setLastName("Kargopolov");

Gson gsonBuilder = new GsonBuilder().create();
String jsonFromPojo = gsonBuilder.toJson(personPojo);

System.out.println(jsonFromPojo);

Convert Java Map into JSON

 Gson gsonBuilder = new GsonBuilder().create();

// Convert Java Map into JSON 
 Map personMap = new HashMap();
 personMap.put("firstName", "Sergey");
 personMap.put("lastName", "Kargopolov");
 String jsonFromJavaMap = gsonBuilder.toJson(personMap);
 
 System.out.println(jsonFromJavaMap);

Convert Java ArrayList into JSON

Gson gsonBuilder = new GsonBuilder().create();
// Convert Java Array into JSON
List languagesArrayList = new ArrayList();
languagesArrayList.add("Russian");
languagesArrayList.add("English");
languagesArrayList.add("French");

String jsonFromJavaArrayList = gsonBuilder.toJson(languagesArrayList);

System.out.println(jsonFromJavaArrayList);

All the above code examples in one single file 

package com.appsdeveloperblog.json.gsonexample;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class App {

    public static void main(String[] args) {
        
        // Create new JSON Object
        JsonObject person = new JsonObject();
        person.addProperty("firstName", "Sergey");
        person.addProperty("lastName", "Kargopolov");

        System.out.println(person.toString());

        // Create Inner JSON Object 
        JsonObject address = new JsonObject();
        address.addProperty("country", "ru");
        address.addProperty("city", "Moscow");

        person.add("address", address);

        System.out.println(person.toString());

        // Create JSON Array 
        JsonArray languages = new JsonArray();
        languages.add("Russian");
        languages.add("English");
        languages.add("French");

        System.out.println(languages.toString());

        // Create JSON Array from String
        String jsonString = "{\"firstName\":\"Sergey\",\"lastName\":\"Kargopolov\"}";
        JsonParser jsonParser = new JsonParser();
        JsonObject objectFromString = jsonParser.parse(jsonString).getAsJsonObject();
 
        System.out.println(objectFromString.toString());
        
        // Convert JSON Array String into JSON Array 
        String jsonArrayString = "[\"Russian\",\"English\",\"French\"]";
        JsonArray arrayFromString = jsonParser.parse(jsonArrayString).getAsJsonArray();
        
        System.out.println(arrayFromString.toString());
        
        
        // Convert JSON Array String into Java Array List
         Gson googleJson = new Gson();
         ArrayList javaArrayListFromGSON = googleJson.fromJson(arrayFromString, ArrayList.class);
         
         System.out.println(javaArrayListFromGSON);
        

        //Convert Java Plain Object into JSON
        Person personPojo = new Person();
        personPojo.setFirstName("Sergey");
        personPojo.setLastName("Kargopolov");

        Gson gsonBuilder = new GsonBuilder().create();
        String jsonFromPojo = gsonBuilder.toJson(personPojo);
        
        System.out.println(jsonFromPojo);

        // Convert Java Map into JSON 
        Map personMap = new HashMap();
        personMap.put("firstName", "Sergey");
        personMap.put("lastName", "Kargopolov");
        String jsonFromJavaMap = gsonBuilder.toJson(personMap);
        
        System.out.println(jsonFromJavaMap);

        // Convert Java Array into JSON
        List languagesArrayList = new ArrayList();
        languagesArrayList.add("Russian");
        languagesArrayList.add("English");
        languagesArrayList.add("French");
        
        String jsonFromJavaArrayList = gsonBuilder.toJson(languagesArrayList);
        
        System.out.println(jsonFromJavaArrayList);
       
    }
}

 

“Complete
icon

Complete Java Masterclass. Video Course.

Learn to master Java 8 core development step-by-step, and make your first unique, advanced program in 30 days.

I would like to have a look

The Complete Java Developer Course
icon

The Complete Java Developer Course. Video Course.

Step by step to learn how to build your apps with Java 8

Start learning Java

JSP, Servlets and JDBC for Beginners: Build a Database App
icon

JSP, Servlets and JDBC for Beginners: Build a Database App. Video Course.

JSP: Covers latest versions of JSP 2.3 and Servlets 3.1 – Most Popular JSP/Servlet course

Lear to Build a Database App