Android6/9/2020

How to Use GSON Library in Android for JSON Parsing

JSON parsing is one of the most important concepts in Android app development.

Most modern Android applications communicate with APIs and servers using JSON data.

Google provides the powerful GSON Library to easily:

  • Convert Java objects into JSON
  • Convert JSON into Java objects
  • Serialize data
  • Deserialize API responses
  • Map JSON keys to Java variables

In this tutorial, we will learn how to use the GSON library in Android Studio with practical examples.


What We Will Learn

  • What is GSON?
  • How to add GSON dependency
  • Convert Java object to JSON
  • Convert JSON to Java object
  • Use @SerializedName annotation
  • Parse JSON efficiently

What is GSON?

GSON is a Java library developed by Google that simplifies:

  • JSON serialization
  • JSON deserialization

GSON automatically maps JSON data to Java classes with minimal code.


Why Use GSON in Android?

GSON is widely used because:

  • Easy to use
  • Fast parsing
  • Lightweight
  • Works well with Retrofit
  • Supports nested JSON
  • Supports custom field names

Step 1 — Add GSON Dependency

Inside:


build.gradle

Add:


implementation 'com.google.code.gson:gson:2.10.1'

Step 2 — Create Employee.java Class

Create:


Employee.java

package com.example.gsonexample;

import com.google.gson.annotations.SerializedName;

public class Employee {

    @SerializedName("first_name")
    private String firstName;

    @SerializedName("age")
    private int age;

    @SerializedName("mail")
    private String email;

    public Employee(
            String firstName,
            int age,
            String email
    ) {

        this.firstName = firstName;
        this.age = age;
        this.email = email;
    }

    public String getFirstName() {
        return firstName;
    }

    public int getAge() {
        return age;
    }

    public String getEmail() {
        return email;
    }
}

What is @SerializedName?

The:


@SerializedName

annotation allows different JSON keys and Java variable names.

Example:


@SerializedName("first_name")
private String firstName;

JSON key:


"first_name"

maps to:


firstName

Step 3 — Convert Java Object to JSON

Inside:


MainActivity.java

package com.example.gsonexample;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;

public class MainActivity
        extends AppCompatActivity {

    @Override
    protected void onCreate(
            Bundle savedInstanceState
    ) {

        super.onCreate(savedInstanceState);

        setContentView(
                R.layout.activity_main
        );

        Gson gson = new Gson();

        Employee employee =
                new Employee(
                        "John",
                        30,
                        "john@gmail.com"
                );

        String json =
                gson.toJson(employee);

        Log.d("JSON_OUTPUT", json);
    }
}

Generated JSON Output


{
  "first_name":"John",
  "age":30,
  "mail":"john@gmail.com"
}

Step 4 — Convert JSON to Java Object


package com.example.gsonexample;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;

public class MainActivity
        extends AppCompatActivity {

    @Override
    protected void onCreate(
            Bundle savedInstanceState
    ) {

        super.onCreate(savedInstanceState);

        setContentView(
                R.layout.activity_main
        );

        Gson gson = new Gson();

        String json =
                "{\"first_name\":\"John\",\"age\":30,\"mail\":\"john@gmail.com\"}";

        Employee employee =
                gson.fromJson(
                        json,
                        Employee.class
                );

        Log.d(
                "EMPLOYEE_NAME",
                employee.getFirstName()
        );
    }
}

How GSON Parsing Works

The workflow is:

  1. JSON string received from API
  2. GSON reads JSON structure
  3. Maps JSON keys to Java variables
  4. Creates Java object automatically

Example JSON File

employee.json


{
  "age": 30,
  "first_name": "John",
  "mail": "john@gmail.com"
}

Real-World Use Cases

GSON is commonly used in:

  • REST API integration
  • Retrofit networking
  • Firebase APIs
  • Local JSON storage
  • Configuration files
  • Caching systems

Modern Android Recommendation

Most modern Android applications combine:

  • Retrofit
  • OkHttp
  • GSON
  • MVVM Architecture
  • Coroutines

for scalable networking systems.


Common Beginner Mistakes

1. Using Wrong JSON Keys

JSON key names must match:


@SerializedName

2. Forgetting Getters

Always create getters if object data must be accessed later.


3. Invalid JSON Format

Malformed JSON causes parsing exceptions.


GSON vs Moshi vs Kotlin Serialization

Library Advantages
GSON Simple and beginner friendly
Moshi Modern and optimized
Kotlin Serialization Best for Kotlin-first apps

Advanced GSON Features

  • Nested object parsing
  • Array parsing
  • Custom serializers
  • Custom deserializers
  • TypeToken support
  • Null handling

FAQ

Can GSON parse API responses?

Yes. GSON is commonly used with Retrofit for parsing API responses automatically.

Is GSON still widely used?

Yes. Although newer alternatives exist, GSON remains very popular in Android development.

Can GSON parse arrays?

Yes. GSON can parse:

  • Lists
  • Arrays
  • Nested objects
  • Complex JSON structures

Conclusion

GSON simplifies JSON serialization and deserialization in Android applications.

It allows developers to convert Java objects and JSON data easily with minimal boilerplate code.

Modern Android applications frequently use GSON with Retrofit and MVVM architecture for scalable networking systems.


About the Author

Salil Jha is a Full Stack and Mobile Developer specializing in Android, React Native, fintech systems, scalable SaaS platforms, and developer tooling products.

CodeChain Dev — Build Modern Products. Solve Real Problems.

Deep Structural Diagnostics.

Mastering JSON is only the first step. Use our industrial-grade workbench to format, validate, and synthesize models for your production APIs.