JSON Fundamentals: A Beginner's Guide to Understanding Data
Learn the basics of JSON (JavaScript Object Notation), the universal data format that powers modern web APIs and applications.
If you’ve ever worked with web APIs, you’ve encountered JSON. It’s the lingua franca of data exchange on the internet, and understanding it is essential for any developer. In this guide, we’ll break down JSON from the ground up.
What is JSON?
JSON stands for JavaScript Object Notation. Despite the name, it’s language-independent and used everywhere – from web APIs to configuration files to databases.
JSON is:
- Human-readable – Easy to write and understand
- Lightweight – Minimal syntax overhead
- Universal – Supported by virtually every programming language
- Structured – Represents complex data relationships
Basic Syntax
JSON has only two structures: objects and arrays.
Objects
Objects are collections of key-value pairs, wrapped in curly braces {}:
{
"name": "Alice",
"age": 30,
"email": "[email protected]"
}
Rules for objects:
- Keys must be strings (in double quotes)
- Key-value pairs are separated by colons
- Pairs are separated by commas
- No trailing commas allowed
Arrays
Arrays are ordered lists of values, wrapped in square brackets []:
["apple", "banana", "cherry"]
Arrays can contain any valid JSON values:
[1, 2, 3, 4, 5]
[true, false, true]
[{"name": "Alice"}, {"name": "Bob"}]
Data Types
JSON supports six data types:
1. Strings
Text wrapped in double quotes:
{
"message": "Hello, World!",
"empty": "",
"with_quotes": "She said \"Hello\""
}
Escape sequences:
-
\"– Double quote -
\\– Backslash -
\n– Newline -
\t– Tab
2. Numbers
Integers or decimals (no quotes):
{
"integer": 42,
"decimal": 3.14159,
"negative": -100,
"scientific": 1.5e10
}
Note: No leading zeros, no hex, no NaN or Infinity.
3. Booleans
True or false (lowercase, no quotes):
{
"is_active": true,
"is_deleted": false
}
4. Null
Represents absence of value:
{
"middle_name": null,
"nickname": null
}
5. Objects
Nested objects for complex structures:
{
"user": {
"name": "Alice",
"address": {
"street": "123 Main St",
"city": "Springfield"
}
}
}
6. Arrays
Lists of values:
{
"colors": ["red", "green", "blue"],
"matrix": [[1, 2], [3, 4]]
}
Real-World Examples
User Profile
{
"id": 12345,
"username": "alice_dev",
"email": "[email protected]",
"profile": {
"first_name": "Alice",
"last_name": "Johnson",
"bio": "Software developer and coffee enthusiast",
"avatar_url": "https://example.com/avatars/alice.jpg"
},
"settings": {
"theme": "dark",
"notifications": true,
"language": "en"
},
"roles": ["user", "admin"],
"created_at": "2024-01-15T10:30:00Z",
"last_login": "2024-02-20T08:45:22Z"
}
API Response
{
"success": true,
"data": {
"products": [
{
"id": 1,
"name": "Widget",
"price": 29.99,
"in_stock": true
},
{
"id": 2,
"name": "Gadget",
"price": 49.99,
"in_stock": false
}
],
"total": 2
},
"meta": {
"page": 1,
"per_page": 10,
"total_pages": 1
}
}
Error Response
{
"success": false,
"error": {
"code": "VALIDATION_ERROR",
"message": "Invalid input data",
"details": [
{
"field": "email",
"message": "Must be a valid email address"
}
]
}
}
Common Mistakes to Avoid
1. Using Single Quotes
// Wrong
{'name': 'Alice'}
// Correct
{"name": "Alice"}
2. Trailing Commas
// Wrong
{
"name": "Alice",
"age": 30,
}
// Correct
{
"name": "Alice",
"age": 30
}
3. Unquoted Keys
// Wrong
{name: "Alice"}
// Correct
{"name": "Alice"}
4. Comments
JSON doesn’t support comments:
// Wrong - this will cause a parse error
{
// User name
"name": "Alice"
}
5. Undefined Values
Use null instead:
// Wrong
{"value": undefined}
// Correct
{"value": null}
Working with JSON in Code
JavaScript
// Parse JSON string to object
const data = JSON.parse('{"name": "Alice"}');
console.log(data.name); // "Alice"
// Convert object to JSON string
const json = JSON.stringify({ name: "Alice" });
console.log(json); // '{"name":"Alice"}'
// Pretty print
const pretty = JSON.stringify({ name: "Alice" }, null, 2);
Python
import json
# Parse JSON string
data = json.loads('{"name": "Alice"}')
print(data['name']) # Alice
# Convert to JSON string
json_str = json.dumps({"name": "Alice"})
print(json_str) # {"name": "Alice"}
# Pretty print
pretty = json.dumps({"name": "Alice"}, indent=2)
Command Line (jq)
# Parse and pretty print
echo '{"name":"Alice"}' | jq .
# Extract a field
echo '{"name":"Alice"}' | jq .name
# Filter arrays
echo '[{"name":"Alice"},{"name":"Bob"}]' | jq '.[0]'
JSON vs Other Formats
| Feature | JSON | XML | YAML |
|---|---|---|---|
| Readability | Good | Moderate | Excellent |
| Verbosity | Low | High | Very Low |
| Comments | No | Yes | Yes |
| Data Types | Basic | Strings only | Rich |
| Parsing Speed | Fast | Slow | Moderate |
JSON hits the sweet spot of being readable, compact, and fast to parse.
JSON Schema
For validating JSON structure, use JSON Schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"name": {
"type": "string",
"minLength": 1
},
"age": {
"type": "integer",
"minimum": 0
},
"email": {
"type": "string",
"format": "email"
}
},
"required": ["name", "email"]
}
This schema ensures your data matches expected structure.
How SheetsJSON Uses JSON
When you connect a Google Sheet to SheetsJSON, we automatically:
- Convert your headers to keys – Column names become JSON property names
- Detect data types – Numbers, booleans, and dates are properly typed
-
Handle empty cells – They become
nullvalues - Structure as arrays – Each row becomes an object in an array
A sheet like this:
| name | age | active |
|---|---|---|
| Alice | 30 | true |
| Bob | 25 | false |
Becomes:
{
"data": [
{"name": "Alice", "age": 30, "active": true},
{"name": "Bob", "age": 25, "active": false}
]
}
Practice Exercises
- Validate JSON: Use JSONLint to check if your JSON is valid
-
Pretty Print: Take minified JSON and format it with
jqor an online tool - Transform Data: Convert a CSV file to JSON manually, then check your work
- API Exploration: Use a public API and examine its JSON responses
Conclusion
JSON is fundamental to modern development. Its simplicity makes it easy to learn, while its flexibility handles complex data structures. Whether you’re building APIs, storing configuration, or exchanging data between services, JSON is likely your go-to format.
Ready to put your JSON knowledge to work? Try SheetsJSON to turn your spreadsheets into JSON APIs instantly. No backend code required!
Further Reading
- JSON.org – Official specification
- JSON Schema – Data validation
- jq Manual – Command-line JSON processor
Happy coding! 🎉
Ready to transform your sheets?
Turn your Google Sheets into powerful JSON APIs in seconds.
Get Started Free