139 lines
3.0 KiB
Markdown
139 lines
3.0 KiB
Markdown
# JSON Cheat Sheet
|
|
|
|
## Table of Contents
|
|
|
|
1. [Introduction to JSON](#introduction-to-json)
|
|
2. [JSON Data Types](#json-data-types)
|
|
|
|
2.1 [String](#string)
|
|
|
|
2.2 [Number](#number)
|
|
|
|
2.3 [Boolean](#boolean)
|
|
|
|
2.4 [Array](#array)
|
|
|
|
2.5 [Object](#object)
|
|
|
|
2.6 [Null](#null)
|
|
|
|
3. [JSON Syntax](#json-syntax)
|
|
4. [JSON Example](#json-example)
|
|
5. [Reading and Writing JSON in Python](#reading-and-writing-json-in-python)
|
|
6. [Accessing JSON Data in JavaScript](#accessing-json-data-in-javascript)
|
|
7. [Accessing JSON Data in PHP](#accessing-json-data-in-php)
|
|
|
|
```json
|
|
{
|
|
"name": "JSON Cheet Sheet",
|
|
"version": "1.1",
|
|
"author": "CheetSheetz",
|
|
"topics": ["JSON", "Data Types", "Syntax", "Example"]
|
|
}
|
|
```
|
|
|
|
## Introduction to JSON
|
|
|
|
- JSON stands for **JavaScript Object Notation**.
|
|
- It is a lightweight data interchange format.
|
|
- JSON is language-independent and easy for humans to read and write.
|
|
- Commonly used for data exchange between a server and web application, as well as for configuration files.
|
|
|
|
## JSON Data Types
|
|
|
|
### String
|
|
|
|
- Represents a sequence of characters.
|
|
- Enclosed in double quotes.
|
|
- Example: `"Hello, World!"`
|
|
|
|
### Number
|
|
|
|
- Represents a numerical value (integer or floating-point).
|
|
- Example: `42` or `3.14`
|
|
|
|
### Boolean
|
|
|
|
- Represents a logical value, either `true` or `false`.
|
|
|
|
### Array
|
|
|
|
- Ordered list of values.
|
|
- Enclosed in square brackets `[]`.
|
|
- Example: `[1, 2, "three", true]`
|
|
|
|
### Object
|
|
|
|
- Unordered collection of key-value pairs.
|
|
- Enclosed in curly braces `{}`.
|
|
- Example: `{"name": "John", "age": 30, "city": "New York"}`
|
|
|
|
### Null
|
|
|
|
- Represents a null or empty value.
|
|
|
|
## JSON Syntax
|
|
|
|
- Data is represented in key-value pairs.
|
|
- Keys are always strings and followed by a colon.
|
|
- Pairs are separated by commas.
|
|
- Objects are enclosed in curly braces `{}`.
|
|
- Arrays are enclosed in square brackets `[]`.
|
|
|
|
## JSON Example
|
|
|
|
```json
|
|
{
|
|
"name": "John Doe",
|
|
"age": 25,
|
|
"isStudent": false,
|
|
"courses": ["Math", "History", "Science"],
|
|
"address": {
|
|
"street": "123 Main St",
|
|
"city": "Anytown",
|
|
"zip": "12345"
|
|
},
|
|
"contact": null
|
|
}
|
|
```
|
|
|
|
## 5. Reading and Writing JSON in Python
|
|
|
|
```python
|
|
import json
|
|
|
|
# Reading JSON from a file
|
|
with open('data.json', 'r') as json_file:
|
|
data = json.load(json_file)
|
|
print(data)
|
|
|
|
# Writing Python data to a JSON file
|
|
data_to_write = {"name": "Jane Doe", "age": 30, "city": "AnotherTown"}
|
|
|
|
with open('output.json', 'w') as json_file:
|
|
json.dump(data_to_write, json_file, indent=4)
|
|
```
|
|
|
|
## Accessing JSON Data in JavaScript
|
|
|
|
```javascript
|
|
// Assuming jsonString is a JSON string
|
|
var jsonData = JSON.parse(jsonString);
|
|
|
|
// Accessing data
|
|
console.log(jsonData.name); // Outputs: John Doe
|
|
console.log(jsonData.address.city); // Outputs: Anytown
|
|
```
|
|
|
|
## Accessing JSON Data in PHP
|
|
|
|
```php
|
|
// Assuming jsonString is a JSON string
|
|
$jsonData = json_decode($jsonString, true);
|
|
|
|
// Accessing data
|
|
echo $jsonData['name']; // Outputs: John Doe
|
|
echo $jsonData['address']['city']; // Outputs: Anytown
|
|
```
|
|
|