What is JSON

What is JSON? Complete Guide with Android Examples (2026) | D.Tech Academic

What is JSON? Complete Guide with Android Examples (2026)

Last Updated: | Author: Deep Singh

If you've ever called a REST API, opened a config file, or stored data in Firebase, you've almost certainly run into JSON. It's the de facto format for moving data between servers, apps, and languages. Short, readable, and universally supported — it's the lingua franca of the web.

In this guide, you'll learn what JSON is, how its syntax works, and how to convert between JSON and Java objects in Android using both Gson and the built-in org.json library. By the end, you'll be comfortable parsing and producing JSON in any Android app.

What is JSON?

JSON stands for JavaScript Object Notation. It's a lightweight, text-based format for representing structured data. Despite the "JavaScript" in the name, it's language-agnostic — you can use it from Java, Python, Go, Rust, or anything else.

You use JSON to:

  • Send data from a server to a mobile app (and back)
  • Store configuration in a file that any tool can read
  • Exchange data between services written in different languages
  • Persist simple structured data locally

JSON is entirely text — it contains no binary data, no proprietary encoding. Just characters. That's why it works everywhere.

JSON Syntax at a Glance

Two rules cover 90% of JSON you'll ever see:

  • A single object is wrapped in curly braces { }
  • A list of objects (an array) is wrapped in square brackets [ ]

Inside an object, data is stored as key-value pairs: the key is always a quoted string, and the value can be a string, number, boolean, null, another object, or an array.

Single Object

{
    "name": "Deep Singh",
    "roll": 1
}

Array of Objects

[
    {
        "name": "Deep Singh",
        "roll": 1
    },
    {
        "name": "Deep Singh",
        "roll": 2
    }
]

A Note on Comments

The original tutorial wrote comments like //single objects directly above the JSON. That's worth clarifying: standard JSON does not support comments. If you paste a comment inside a JSON document, a strict parser will reject it.

Some tools (like VS Code's settings and JSON5) allow comments, but they're not part of the JSON spec. Keep comments out of your JSON files — use a separate documentation file if you need to annotate.

Converting a Java Object to JSON

To convert between Java and JSON, you need a library. You have two good options:

  • Gson — a popular third-party library from Google. Simple API, widely used, handles almost any object graph out of the box.
  • org.json — a small library bundled with Android. Lower-level API, but no extra dependency.

This section uses Gson because it's the friendliest for beginners.

Add Gson to Your Project

In build.gradle (module level), add:

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

Then sync your project.

Define the Model Class

public class Student {
    private int roll;
    private String name;
    private float marks;

    // getters and setters
    public int getRoll() { return roll; }
    public void setRoll(int roll) { this.roll = roll; }

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }

    public float getMarks() { return marks; }
    public void setMarks(float marks) { this.marks = marks; }
}

Convert One Object to JSON

Student s = new Student();
s.setRoll(1);
s.setName("Deep Singh");
s.setMarks(78.8f);

Gson gson = new Gson();
String json = gson.toJson(s);

Log.d("JSON", json);

Output:

{"roll":1,"name":"Deep Singh","marks":78.8}

gson.toJson() inspects the object's fields and produces the corresponding JSON string. That's it — no configuration needed.

Converting a List of Java Objects to JSON

The same pattern works for lists. Just pass the list to gson.toJson():

Student s1 = new Student();
s1.setRoll(1);
s1.setName("Deep Singh");
s1.setMarks(78.8f);

Student s2 = new Student();
s2.setRoll(2);
s2.setName("Deep Singh 1");
s2.setMarks(78.4f);

List<Student> students = new ArrayList<>();
students.add(s1);
students.add(s2);

Gson gson = new Gson();
String json = gson.toJson(students);

Log.d("JSON", json);

Output:

[{"roll":1,"name":"Deep Singh","marks":78.8},{"roll":2,"name":"Deep Singh 1","marks":78.4}]

Gson automatically wraps the list in square brackets and produces one JSON object per element. This is exactly the format most REST APIs expect.

Converting JSON Back to Java Objects

Parsing works the same way in reverse. Here are two approaches: the manual org.json way (which teaches you what's happening under the hood), and the Gson way (which is what you'll actually use in production).

Method 1: Manual Parsing with org.json

This approach walks through the JSON structure explicitly. It's more code, but it makes the parsing mechanics clear.

try {
    JSONArray jsonArray = new JSONArray(json);
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject obj = jsonArray.getJSONObject(i);

        int roll = obj.getInt("roll");
        String name = obj.getString("name");
        float marks = (float) obj.getDouble("marks");

        Student s = new Student();
        s.setRoll(roll);
        s.setName(name);
        s.setMarks(marks);

        Log.d("Student", s.getRoll() + "\t" + s.getName());
    }
} catch (JSONException e) {
    Log.e("JSON", "Parsing failed", e);
}

Important: In the original tutorial, the catch block caught generic Exception. That works, but it hides unrelated problems. Catch JSONException specifically — it's the one that JSONArray and JSONObject throw.

Method 2: Automatic Parsing with Gson

Gson can do the entire loop for you:

Gson gson = new Gson();
Type listType = new TypeToken<List<Student>>(){}.getType();
List<Student> students = gson.fromJson(json, listType);

for (Student s : students) {
    Log.d("Student", s.getRoll() + "\t" + s.getName());
}

The tricky part is the TypeToken. Java erases generics at runtime, so Gson can't know whether you want List<Student> or List<String> from the class alone. TypeToken captures the generic type so Gson knows what to build.

If you're parsing a single object instead of a list, you can skip TypeToken:

Student s = gson.fromJson(json, Student.class);

Warning from the original tutorial: The original showed gson.fromJson(jo.toString(), Student.class) inside a loop. That works, but it's a strange hybrid — using Gson to convert one JSONObject at a time instead of the whole array. The TypeToken approach above is cleaner and faster.

Gson vs org.json: Which One Should You Use?

Both libraries work. Here's a quick comparison:

  • Gson — third-party, one line to add. Automatically converts between Java objects and JSON with no per-field code. Handles nested objects and generics cleanly. Recommended for almost all use cases.
  • org.json — bundled with Android, no extra dependency. Lower-level: you call getInt(), getString(), etc. manually. Useful when you need fine control or want to avoid a dependency, but tedious for anything complex.

For a new project in 2026, use Gson. If you're already using Moshi or Kotlin Serialization in your project, use whatever your project already standardised on — they're all excellent.

Modern alternatives to consider:

  • Moshi — similar to Gson but with better Kotlin support.
  • Kotlin Serialization — the official Kotlin solution. Compile-time safe, no reflection.
  • Jackson — the most feature-rich Java JSON library, popular in Spring Boot projects.

Wrapping Up

You now know what JSON is, how its syntax works, and how to convert between JSON and Java objects in Android. The key points:

  • JSON is a text-based, language-agnostic format.
  • Objects use { }, arrays use [ ].
  • Data is stored as key-value pairs, all keys are quoted strings.
  • Standard JSON does not support comments.
  • Gson handles most conversions in one line — gson.toJson(obj) and gson.fromJson(json, Class.class).
  • For lists, use TypeToken to capture the generic type.
  • Gson is almost always the better choice over manual org.json parsing.

From here, you can dive into:

  • Retrofit — the standard Android HTTP client, which integrates beautifully with Gson.
  • Moshi or Kotlin Serialization — modern replacements for Gson, especially in Kotlin projects.
  • Firebase / REST APIs — where JSON is the default data format you'll be producing and consuming.

You might also find these helpful:

1 comment:

Anonymous said...

nice explanation