JSON?

json

  • JSON (JavaScript Object Notation) is described as a shorthand way of saying JavaScript object notation
  • It is presented as a method to store and send data in a format that is easy for both humans to read and computers to understand
  • Although ‘JavaScript’ is in the name, JSON is not limited to JavaScript and works with almost every programming language, including Python, Java, and PHP
  • It is considered a simplified way to represent data

JSON Structure

Transclude of JSON-101-2025-06-04-21.44.20.excalidraw

  • Curly braces {} are used to define an object
  • Inside the curly braces, data is stored as key-value pairs.
  • Keys are always enclosed in double quotes.
  • Numerical values (like numbers) do not need to be enclosed in quotes, allowing JSON to differentiate between text and numerical data
  • Each key-value pair is separated by a comma, except for the last one within the object
  • JSON also supports arrays, which are enclosed in square brackets [] and can contain multiple values separated by commas, Values within an array can be strings, numbers, or even other objects or arrays
  • For a file to be recognised as a JSON document and interpreted properly by programs, it needs the .json file extension

Super-powered JSON (Nesting)

superpower

{
"name": "Jake",
"age": 25,
"address": {
	"street": "123 Main St",
	"city": "New York",
	"zip": "10001",
	"coordinates": {
	"latitude": 40.7128,
	"longitude": -74.0060
"hobbies": ["Swimming", "Basketball"]
}

A powerful aspect of JSON is its ability to nest data

  • An object can contain another object as a value, This is done by defining a key and assigning an entire object as its value
    • This approach is useful for representing structured data with multiple levels
  • Deep nesting is useful for representing structured information like geographical data
  • Nesting allows JSON to naturally represent relationships between data in a hierarchical manner, making it ideal for real-world applications such as user profiles, product catalogues, and API responses
  • Instead of using multiple separate objects, nesting allows for improved organisation, enhancing readability and maintaining a clear structure, Many APIs use nested JSON structures to return complex data, This structure makes it easy to access and manage different levels of information efficiently

Accessing JSON

To work with JSON data, you need a programming language because JSON is just text

In JavaScript

  • JSON is typically converted into an object using the JSON.parse method
  • If fetching data using fetch, the response can be converted immediately using the .json() function
{
product_name: "Acer Nitro 5",
brand: "Acer",
price: 999.99,
specs: {
	processor:
	"Intel Core i7",
	ram:
	"16GB",
	storage: "512GB SSD" ,
	graphics:
	"NVIDIA RTX 3060",
	}
};
  • Data can then be retrieved using dot notation (object.key) or bracket notation (object[“key”])
  • Nested data can be accessed by chaining the notation, e.g., object.specs.storage
  • In Python, JSON data is parsed into a dictionary using the json module and specifically the json.loads() method
  • Once parsed, data can be accessed and manipulated just like a normal Python dictionary using bracket notation (dictionary[“key”])