Chapter 1.3: JSON Format
Study time: 35 minutes
1. JSON: The Language of Space Communication
Imagine that a space probe and the MCC speak different languages. JSON (JavaScript Object Notation) is a universal data language that is understandable to both humans and machines. Like the international language of astronauts!
Main properties:
- 🪶 Lightweight: fast transmission even with a weak signal
- 👀 Human-readable: the structure is visible to the naked eye
- 🔄 Universal: supported by all programming languages
💡 Space analogy:
JSON is like a digital telegram with data about stars. Instead of Morse code, there are clear
{key: value}
structures.
2. Anatomy of a JSON message
JSON consists of two types of structures:
A. Objects (Spaceships)
B. Arrays (Star clusters)
[
{"name": "Sirius", "magnitude": -1.46},
{"name": "Canopus", "magnitude": -0.74},
{"name": "Arcturus", "magnitude": -0.05}
]
⚡ Syntax rules:
- Keys in double quotes " "
- Values: strings, numbers, boolean (
true
/false
),null
- Data is separated by commas
- Curly braces
{}
for objects, square brackets[]
for arrays
3. JSON vs XML: The Battle of Formats
Parameter | JSON (Modern satellite) | XML (Old telescope) |
---|---|---|
Readability | High (direct access to fields) | Low (tags <planet>...</planet> ) |
Size | Compact | Bulky (+30% to size) |
Processing | Fast | Slow |
Example | {"planet": "Mars"} |
<planet>Mars</planet> |
🔭 Why did JSON win in space?
The NASA, SpaceX, and ESA APIs use JSON - it's ideal for data transmission!
4. Working with JSON in code
Python example (getting data about the Moon):
import requests
import json
# We use a real API, for example, to get data about the Solar System
# from The Solar System OpenData API
try:
response = requests.get("https://api.le-systeme-solaire.net/rest/bodies/terre")
response.raise_for_status() # Check for HTTP errors (4xx, 5xx)
planet_data = response.json()
print(f"Name: {planet_data['englishName']}")
print(f"Satellites: {[moon['moon'] for moon in planet_data['moons']]}")
# Convert a Python dictionary to a JSON string for saving
planet_json_str = json.dumps(planet_data, indent=2, ensure_ascii=False) # ensure_ascii=False for Cyrillic
print("\n--- Formatted JSON ---")
print(planet_json_str)
except requests.exceptions.RequestException as e:
print(f"Request error: {e}")
JavaScript example (processing telescope data):
const Data = `{
"id": "terre",
"name": "La Terre",
"englishName": "Earth",
"isPlanet": true,
"moons": [
{
"moon": "La Lune",
"rel": "https://api.le-systeme-solaire.net/rest/bodies/lune"
}
],
"semimajorAxis": 149598023,
"perihelion": 147095000,
"aphelion": 152100000,
"eccentricity": 0.0167,
"inclination": 0,
"mass": {
"massValue": 5.97237,
"massExponent": 24
},
"vol": {
"volValue": 1.08321,
"volExponent": 12
},
"density": 5.5136,
"gravity": 9.8,
"escape": 11190,
"meanRadius": 6371.0084,
"equaRadius": 6378.1366,
"polarRadius": 6356.8,
"flattening": 0.00335,
"dimension": "",
"sideralOrbit": 365.256,
"sideralRotation": 23.9345,
"aroundPlanet": null,
"discoveredBy": "",
"discoveryDate": "",
"alternativeName": "",
"axialTilt": 23.4393,
"avgTemp": 288,
"mainAnomaly": 358.617,
"argPeriapsis": 85.901,
"longAscNode": 18.272,
"bodyType": "Planet"
}`;
const dataObj = JSON.parse(Data);
console.log(dataObj.target);
5. JSON Validation: Checking the "Signal"
Before using the data, make sure the JSON:
-
Has the correct structure (check the brackets!)
-
Contains the expected fields
-
Has no syntax errors
Tools: - JSONLint (like a space debris detector for JSON) - Built-in language parsers (will throw an error for an incorrect format)
⚠️ Example of an error:
{"planet": "Mars"
→ No closing bracket!Solution: Always use
json.loads()
in Python orJSON.parse()
in JS for validation.
Quiz for reinforcement
🚀 Chapter summary:
JSON is the "space Esperanto" of the API world. Knowing its structure, you can:
-
📡 Receive data from telescopes and probes
-
🔧 Convert information between systems
-
🛰️ Save scientific data in files
Get ready for docking! In the next chapter, we will study the REST API architecture - the control system of your server's "space station".
📌 Practice: Go to NASA Open APIs, find the Asteroids NeoWs API, and study the JSON structure in the response!