coderolls Staff
coderolls Staff Hi 👋🏼, At coderolls we write tutorials about Java programming language and related technologies.

How To Parse JSON In Java?

In this article, we will see, how to parse JSON in Java. We will be using the JSON Libraries like JSON-java, GSON and json-simple

Introduction

JSON stands for JavaScript Object Notation. It is easy to use and lightweight open standard file format for storing and transporting data.

As per the json.org,

It is easy for humans to read and write. It is easy for machines to parse and generate.

Example JSON:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
  "name": "coderolls",
  "type": "blog",
  "address": {
    "street": "1600 Pennsylvania Avenue NW",
    "city": "Washington",
    "state": "DC"
  },
  "employees": [
    {
      "firstName": "John",
      "lastName": "Doe"
    },
    {
      "firstName": "Anna",
      "lastName": "Smith"
    },
    {
      "firstName": "Peter",
      "lastName": "Jones"
    }
  ]
}

In the above JSON, name is the key and coderolls is its value. For the address key, the value is another JSON object which contains the key and values.

For the employees key, its value is an array of JSON objects.

Today we will see three JSON libraries in Java to parse a JSON string. These are listed below,

  1. JSON-Java
  2. GSON
  3. json-simple

Now we will see one by one examples of all three libraries for parsing a JSON in Java.

How to parse JOSN in Java using JSON-Java

JSON-java is one of the most simple JSON libraries for Java.

Here, we will be using the JSONObject class of the JSON-java library.

JSONObject has a constructor which accepts string. We will be using this constructor to parse a JSON string.

1
2
3
4
5
6
7
8
9
10
11
public JSONObject(String source) throws JSONException

Construct a JSONObject from a source JSON text string. This is the most commonly used JSONObject constructor.

Parameters:

source  - A string beginning with  `{` (left brace)  and ending with  `}`  (right brace).

Throws:

JSONException  - If there is a syntax error in the source string or a duplicated key.

You can refer JSON-java Docs.

Maven:

1
2
3
4
5
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20201115</version>
</dependency>

Gradle:

1
compile group: 'org.json', name: 'json', version: '20201115'

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.coderolls.JSONExample;

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

/**
 * A program to parse JSON string in Java using JSON-Java
 * @author Gaurav Kukade at coderolls.com
 */

public class ParseJSONUsingJSONJava {

  public static void main(String[] args) {
  
    String jsonString = "{"
        + "  \"name\": \"coderolls\","
        + "  \"type\": \"blog\","
        + "  \"address\": {"
        + "    \"street\": \"1600 Pennsylvania Avenue NW\","
        + "    \"city\": \"Washington\","
        + "    \"state\": \"DC\""
        + "  },"
        + "  \"employees\": ["
        + "    {"
        + "      \"firstName\": \"John\","
        + "      \"lastName\": \"Doe\""
        + "    },"
        + "    {"
        + "      \"firstName\": \"Anna\","
        + "      \"lastName\": \"Smith\""
        + "    },"
        + "    {"
        + "      \"firstName\": \"Peter\","
        + "      \"lastName\": \"Jones\""
        + "    }"
        + "  ]"
        + "}";
    
    System.out.println("Parsing the JSON string in Java using JSON-Java......\n");
    
    //add jsonString to the constructor
    JSONObject coderollsJSONObject = new JSONObject(jsonString);
    
    //now we can access the values 
    String name = coderollsJSONObject.getString("name");
    System.out.println("Name: "+name+"\n");
    
    //we can get the JSON object present as value of any key in the parent JSON
    JSONObject addressJSONObject = coderollsJSONObject.getJSONObject("address");
    
    //access the values of the addressJSONObject 
    String street = addressJSONObject.getString("street");
    System.out.println("Street: "+street+"\n");
    
    
    //we can get the JSON array present as the value of any key in the parent JSON
    JSONArray employeesJSONArray = coderollsJSONObject.getJSONArray("employees");
    System.out.println("Printing the employees JSON array: \n"+employeesJSONArray.toString()+"\n");
    
    //we can get individual JSON objects at an index from the employeesJSONArray
    JSONObject employeeJSONObject = employeesJSONArray.getJSONObject(0);
    String firstName = employeeJSONObject.getString("firstName");
    System.out.println("First Name of the employee at index 0: "+firstName);
  }
}

Get the code for ParseJSONUsingJSON.Java

Output:

1
2
3
4
5
6
7
8
9
10
Parsing the JSON string in Java using JSON-Java......

Name: coderolls

Street: 1600 Pennsylvania Avenue NW

Printing the employee's JSON array: 
[{"firstName":"John","lastName":"Doe"},{"firstName":"Anna","lastName":"Smith"},{"firstName":"Peter","lastName":"Jones"}]

First Name of the employee at index 0: John

How to parse JOSN in Java using Gson

Gson is an open-source Java library to serialize and deserialize Java objects to JSON developed at Google.

So we can use it to convert a JSON string to an equivalent Java object.

Here, we will be using the JsonObject class of the Gson library.

You can refer to the docs of JsonObject.

Maven:

1
2
3
4
5
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.6</version>
</dependency>

Gradle:

1
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.6'

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.coderolls.JSONExample;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

/**
 * A program to parse JSON string in Java using Gson
 * @author Gaurav Kukade at coderolls.com
 */

public class ParseJSONUsingGSON {

  public static void main(String[] args) {
  
    //Take JSON as a string
    String jsonString = "{"
        + "  \"name\": \"coderolls\","
        + "  \"type\": \"blog\","
        + "  \"address\": {"
        + "    \"street\": \"1600 Pennsylvania Avenue NW\","
        + "    \"city\": \"Washington\","
        + "    \"state\": \"DC\""
        + "  },"
        + "  \"employees\": ["
        + "    {"
        + "      \"firstName\": \"John\","
        + "      \"lastName\": \"Doe\""
        + "    },"
        + "    {"
        + "      \"firstName\": \"Anna\","
        + "      \"lastName\": \"Smith\""
        + "    },"
        + "    {"
        + "      \"firstName\": \"Peter\","
        + "      \"lastName\": \"Jones\""
        + "    }"
        + "  ]"
        + "}";
    System.out.println("Parsing the JSON string in Java using Gson......\n");
    
    Gson gson = new Gson();
    
    //get JSON object from the JSON string
    JsonObject coderollsJsonObject = gson.fromJson(jsonString, JsonObject.class);
    
    //now we can access the values 
    String name = coderollsJsonObject.get("name").getAsString();
    System.out.println("Name: "+name+"\n");
    
    //we can get the JSON object present as the value of any key in the parent JSON
    JsonObject addressJsonObject = coderollsJsonObject.get("address").getAsJsonObject();
    
    //access the values of the addressJSONObject 
    String street = addressJsonObject.get("street").getAsString();
    System.out.println("Street: "+street+"\n");
    
    
    //we can get the JSON array present as the value of any key in the parent JSON
    JsonArray employeesJsonArray = coderollsJsonObject.get("employees").getAsJsonArray();
    System.out.println("Printing the employees JSON array: \n"+employeesJsonArray.toString()+"\n");
    
    //we can get individual JSON object at an index from the employeesJSONArray
    JsonObject employeeJsonObject = employeesJsonArray.get(0).getAsJsonObject();
    String firstName = employeeJsonObject.get("firstName").getAsString();
    System.out.println("First Name of the employee at index 0: "+firstName);
  }
}

Get the code for ParseJSONUsingGSON.java

Output:

1
2
3
4
5
6
7
8
9
10
Parsing the JSON string in Java using Gson......

Name: coderolls

Street: 1600 Pennsylvania Avenue NW

Printing the employees JSON array: 
[{"firstName":"John","lastName":"Doe"},{"firstName":"Anna","lastName":"Smith"},{"firstName":"Peter","lastName":"Jones"}]

First Name of the employee at index 0: John

How to parse JOSN in Java using json-simple

JSON.simple is a simple Java toolkit for JSON. You can use JSON.simple to encode or decode JSON text.

Maven:

1
2
3
4
5
<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>

Gradle:

1
compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'

You can refer to the json-simple project page.java.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.coderolls.JSONExample;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/**
 * A program to parse JSON string in Java using json-simple
 * @author Gaurav Kukade at coderolls.com
 */

public class ParseJSONUsingJsonSimple {

  public static void main(String[] args) {
    //Take JSON as string
    String jsonString = "{"
            + "  \"name\": \"coderolls\","
            + "  \"type\": \"blog\","
            + "  \"address\": {"
            + "    \"street\": \"1600 Pennsylvania Avenue NW\","
            + "    \"city\": \"Washington\","
            + "    \"state\": \"DC\""
            + "  },"
            + "  \"employees\": ["
            + "    {"
            + "      \"firstName\": \"John\","
            + "      \"lastName\": \"Doe\""
            + "    },"
            + "    {"
            + "      \"firstName\": \"Anna\","
            + "      \"lastName\": \"Smith\""
            + "    },"
            + "    {"
            + "      \"firstName\": \"Peter\","
            + "      \"lastName\": \"Jones\""
            + "    }"
            + "  ]"
            + "}";
        
    System.out.println("Parsing the JSON string in Java using json-simple......\n");
    
    JSONParser parser = new JSONParser();
    JSONObject coderollsJSONObject = new JSONObject();
    try {
      coderollsJSONObject = (JSONObject) parser.parse(jsonString);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    
    //now we can access the values 
    String name = (String) coderollsJSONObject.get("name");
    System.out.println("Name: "+name+"\n");
    
    //we can get the JSON object present as the value of any key in the parent JSON
    JSONObject addressJSONObject = (JSONObject) coderollsJSONObject.get("address");
    
    //access the values of the addressJSONObject 
    String street = (String) addressJSONObject.get("street");
    System.out.println("Street: "+street+"\n");
    
    
    //we can get the JSON array present as the value of any key in the parent JSON
    JSONArray employeesJSONArray = (JSONArray) coderollsJSONObject.get("employees");
    System.out.println("Printing the employees JSON array: \n"+employeesJSONArray.toString()+"\n");
    
    //we can get individual JSON objects at an index from the employeesJSONArray
    JSONObject employeeJSONObject = (JSONObject) employeesJSONArray.get(0);
    String firstName = (String) employeeJSONObject.get("firstName");
    System.out.println("First Name of the employee at index 0: "+firstName);
  }
}

Get the code for ParseJSONUsingJsonSimple.java

Output:

1
2
3
4
5
6
7
8
9
10
Parsing the JSON string in Java using json-simple......

Name: coderolls

Street: 1600 Pennsylvania Avenue NW

Printing the employees JSON array: 
[{"firstName":"John","lastName":"Doe"},{"firstName":"Anna","lastName":"Smith"},{"firstName":"Peter","lastName":"Jones"}]

First Name of the employee at index 0: John

Conclusion

All three libraries allow you to create a JSON object from the JSON string but I found the JSON-Java is easy to use.

You can visit my YouTube channel ‘coderolls’ to find more video tutorials.

Join Newsletter
Get the latest tutorials right in your inbox. We never spam!

comments powered by Disqus