CodeGym /Courses /JAVA 25 SELF /JSON basics: structure, syntax, advantages

JSON basics: structure, syntax, advantages

JAVA 25 SELF
Level 46 , Lesson 0
Available

1. What is JSON (JavaScript Object Notation)?

JSON (pronounced “jay-son,” not “jeh-son”!) stands for JavaScript Object Notation. Despite the word “JavaScript” in the name, JSON is a universal format supported by virtually all programming languages, including Java.

JSON was created by Douglas Crockford in the early 2000s as a simple way to exchange data between the browser and the server. Before that, XML prevailed — verbose and tag-heavy.

JSON turned out to be more compact, simpler, and easy to read “with your eyes.” It quickly became the de facto standard for data exchange between client and server, especially on the web and in mobile applications.

Why did JSON become the standard?

  • Simplicity of syntax — easy to write and read by hand.
  • Compactness — no extra tags or fluff.
  • Readability — even a non-technical person can understand what’s going on.
  • Ease of parsing — most languages have built-in/third‑party libraries.
  • Cross-language — Java, Python, Kotlin, etc. — everyone supports JSON.
  • Standard for REST APIs — the vast majority of APIs return JSON.

Comparison with XML

JSON XML
Readability Very high Medium (lots of tags)
Size Compact More bulky
Parsing Simple Requires more effort
Extensibility Supports nesting Supports nesting
Strictness Less formal Can describe schemas
Popularity (2020+) Very high Used, but less often

JSON wins almost across the board. Nevertheless, XML remains relevant where complex schemas, strict typing, XSD validation, comments, and attributes are needed — for example, in legacy systems and banking software.

In practice, if you’re not writing software for a bank or a spacecraft, use JSON.

2. Basic JSON syntax

If you have worked with JavaScript objects or dictionaries in other languages, the syntax will look familiar. In Java there is no direct “dictionary”; its role is played by collections from java.util, primarily Map<K, V> (most often — HashMap<K, V>).

Core elements

  • Objects — a set of “key: value” pairs wrapped in { }.
  • Arrays — ordered lists of values wrapped in [ ].

Example of an object:

{
  "name": "Alice",
  "age": 25
}

Example of an array:

[1, 2, 3, 4, 5]

Example of an object with an array:

{
  "students": [
    "Ivan",
    "Maria",
    "John"
  ]
}

Allowed value types

JSON allows only the following data types:

Type Example Description
String
"hello"
Always in double quotes
Number
123, 3.14, -7
Integers and decimals
Boolean
true, false
Logical values
null
null
“Empty”, absence of a value
Object
{ ... }
Set of “key: value” pairs
Array
[ ... ]
List of values

Note: keys in objects are always strings in double quotes, for example "name". Single quotes are not allowed.

Examples of valid JSON

{
  "id": 1,
  "name": "Bob",
  "active": true,
  "scores": [10, 20, 30],
  "profile": {
    "email": "bob@example.com",
    "phone": null
  }
}

Example of invalid JSON (errors)

{
  name: 'Bob', // Error: keys and strings must use double quotes only!
  age: 25,
}

3. Data structure: nesting of objects and arrays

JSON supports deep nesting: objects in objects, arrays in objects, objects in arrays, and so on. It’s like a matryoshka.

Example of a complex JSON document

{
  "university": "Java University",
  "students": [
    {
      "name": "Ivan",
      "age": 20,
      "courses": ["Math", "Java", "English"]
    },
    {
      "name": "Maria",
      "age": 19,
      "courses": ["Biology", "Java", "Art"]
    }
  ],
  "active": true
}

In the root object there is a string field "university", a boolean field "active", and an array "students" that contains student objects.

Visual schema (block diagram)

{
  "key": value,
  "array": [
    { "key": value },
    { "key": value }
  ],
  "object": {
    "key": value
  }
}

4. Advantages of JSON

Why is JSON so popular?

  1. Easy for humans and machines to read — the structure is easy to grasp.
  2. Compactness — minimal extra characters.
  3. Simple parsing — many libraries for reading/writing.
  4. Standard for REST APIs and mobile apps — used everywhere.
  5. Flexibility — describes both simple lists and tree structures.
  6. Configs, settings, data storage — often used in configurations.
  7. Supported by all languages — Java, Python, Go, Rust, and more.

5. Practice: walking through JSON file examples

Example 1: List of users

[
  {
    "id": 1,
    "name": "Alice"
  },
  {
    "id": 2,
    "name": "Bob"
  }
]

This is an array where each element is a user object.

Example 2: Application configuration

{
  "debug": true,
  "maxConnections": 100,
  "database": {
    "host": "localhost",
    "port": 5432,
    "user": "admin",
    "password": "qwerty"
  }
}

An object with a nested "database" object. Very similar to real application settings.

Example 3: Errors in JSON

{
  "name": "Alice",
  "age": 25,
} // Error: trailing comma before the closing brace!

Remember: in JSON you cannot put a comma after the last element.

Example 4: Keys without quotes

{ name: "Alice", age: 25 } // Error: keys must be in double quotes!

6. Useful nuances

Table: what is allowed and what is not in JSON

Allowed Not allowed
"key": "value"
'key': 'value'
"key": 123
"key": 0123
"key": null
"key": undefined
"key": true
"key": True
"key": [1, 2, 3]
"key": [1, 2, 3,]

Quick comparison of JSON and Java objects

Java JSON
int, double, boolean
Number, true/false
String
"string"
List<T>, array [value1, value2, ...]
Map<String, Object>
{ "key": value, ... }
null
null
Class with fields { "field": value, ... }

Example: a Java class and its JSON representation

public class Student {
    public String name;
    public int age;
}
{
  "name": "Alice",
  "age": 20
}

7. Typical mistakes when working with JSON

Error #1: Single quotes instead of double quotes. JSON accepts only double quotes for keys and strings.

{ 'name': 'Alice' } // Error!

Error #2: Trailing comma. You cannot put a comma after the last element.

{ "name": "Alice", } // Error!

Error #3: Keys without quotes. Keys must always be in double quotes.

{ name: "Alice" } // Error!

Error #4: Numbers with leading zeros. Such numbers are not allowed.

{ "age": 025 } // Error!

Error #5: Using undefined. JSON has no undefined; use null.

{ "value": undefined } // Error!

Error #6: True/False capitalized. In JSON only true and false (lowercase) are allowed.

{ "active": True } // Error!
1
Task
JAVA 25 SELF, level 46, lesson 0
Locked
Hero's magical data 🧙‍♂️
Hero's magical data 🧙‍♂️
1
Task
JAVA 25 SELF, level 46, lesson 0
Locked
Creating a Card for a Digital Library 📚
Creating a Card for a Digital Library 📚
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION