JavaScript Object Notation, is a lightweight, text-based format used for storing and transporting data. It is the "language of the web" because it is easy for humans to read and write, and easy for machines to parse and generate.
Language Independent: Although it originated from JavaScript, almost every modern programming language (Python, Java, C++, PHP, etc.) has libraries to parse JSON.
Self-Describing: The structure makes the data's meaning relatively easy to understand just by looking at it.
Lightweight: It has a very low overhead compared to older formats like XML.
To be valid JSON, the data must follow these strict rules:
Data is in name/value pairs (e.g., "name": "John").
Data is separated by commas.
Curly braces {} hold objects.
Square brackets [] hold arrays.
Double quotes "" must be used for both keys and string values.
JSON supports a specific set of data types:
Type Description Example
String Text wrapped in double quotes. "Hello World"
Number Integer or floating point. 42 or 3.14
Object A collection of key/value pairs. {"id": 1}
Array An ordered list of values. ["apple", "banana"]
Boolean True or false values. true or false
Null An empty value. null
Here is how a complex JSON object looks:
JSON
{
"company": "TechCorp",
"active": true,
"location": {
"city": "San Francisco",
"zip": 94105
},
"employees": [
{
"name": "Alice",
"role": "Developer"
},
{
"name": "Bob",
"role": "Designer"
}
]
}
Before JSON became the standard, XML was the primary format for data exchange. Here is why JSON won:
Simplicity: JSON is much less "wordy" than XML.
Speed: Because it is smaller, it transmits faster over networks.
Parsing: JSON can be parsed into a ready-to-use JavaScript object with one line of code: JSON.parse(data). XML requires a complex DOM parser.
APIs: Most web APIs (like Twitter, Google Maps, or GitHub) send data to your browser or app in JSON format.
Configuration Files: Many tools (like VS Code or Node.js) use JSON to store settings.
NoSQL Databases: Databases like MongoDB store data in a JSON-like format (BSON).
This JSON object defines the inputs, the operation to perform, and the desired output targets.
JSON
{
"calculation": {
"inputs": {
"value1": 10,
"value2": 5
},
"operation": "multiplication",
"output_targets": [
"screen",
"printer"
]
}
}
Because JSON is static, you need a "parser" to make it come to life. Here is a conceptual flow of how a system would handle the JSON above:
Read: The program opens the JSON file.
Calculate: It identifies value1 and value2 and multiplies them.
Route: It checks the output_targets.
Execute: It sends the result to the monitor and triggers a print job.
If you were running this in a web browser, the code to handle that JSON would look like this:
JavaScript
// The JSON data
const data = {
"inputs": { "v1": 10, "v2": 5 },
"targets": ["screen", "printer"]
};
// 1. Perform the multiplication
const result = data.inputs.v1 * data.inputs.v2;
// 2. Output to screen
if (data.targets.includes("screen")) {
console.log("Result:", result);
alert("The result is: " + result);
}
// 3. Output to printer
if (data.targets.includes("printer")) {
window.print();
}