Added JSON sheet.
This commit is contained in:
parent
0f19569ace
commit
ac693a0111
|
|
@ -0,0 +1,133 @@
|
||||||
|
# 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 Cheat Sheet",
|
||||||
|
"version": "1.1",
|
||||||
|
"author": "OpenAI",
|
||||||
|
"topics": ["JSON", "Data Types", "Syntax", "Example"]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 1. 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.
|
||||||
|
|
||||||
|
## 2. JSON Data Types
|
||||||
|
|
||||||
|
### 2.1 String
|
||||||
|
|
||||||
|
- Represents a sequence of characters.
|
||||||
|
- Enclosed in double quotes.
|
||||||
|
- Example: `"Hello, World!"`
|
||||||
|
|
||||||
|
### 2.2 Number
|
||||||
|
|
||||||
|
- Represents a numerical value (integer or floating-point).
|
||||||
|
- Example: `42` or `3.14`
|
||||||
|
|
||||||
|
### 2.3 Boolean
|
||||||
|
|
||||||
|
- Represents a logical value, either `true` or `false`.
|
||||||
|
|
||||||
|
### 2.4 Array
|
||||||
|
|
||||||
|
- Ordered list of values.
|
||||||
|
- Enclosed in square brackets `[]`.
|
||||||
|
- Example: `[1, 2, "three", true]`
|
||||||
|
|
||||||
|
### 2.5 Object
|
||||||
|
|
||||||
|
- Unordered collection of key-value pairs.
|
||||||
|
- Enclosed in curly braces `{}`.
|
||||||
|
- Example: `{"name": "John", "age": 30, "city": "New York"}`
|
||||||
|
|
||||||
|
### 2.6 Null
|
||||||
|
|
||||||
|
- Represents a null or empty value.
|
||||||
|
|
||||||
|
## 3. 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 `[]`.
|
||||||
|
|
||||||
|
## 4. 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)
|
||||||
|
```
|
||||||
|
|
||||||
|
## 6. 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
|
||||||
|
```
|
||||||
|
|
||||||
|
## 7. 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
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
20
README.md
20
README.md
|
|
@ -1,18 +1,14 @@
|
||||||
# CheetSheetz
|
# CheetSheetz
|
||||||
|
|
||||||
## Personal composition of various technical "cheat sheets".
|
### A compendium of various technical "cheat sheets".
|
||||||
|
|
||||||
[JavaScript](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/javascript.md)
|
- [JavaScript](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/javascript.md)
|
||||||
|
- [JavaScript Object Notation](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/JSON.md)
|
||||||
[Markdown](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/markdown.md)
|
- [Markdown](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/markdown.md)
|
||||||
|
- [Perl](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/perl.md)
|
||||||
[Perl](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/perl.md)
|
- [PHP](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/PHP.md)
|
||||||
|
- [Python](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/python.md)
|
||||||
[PHP](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/PHP.md)
|
- [LEMP Guide](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/LEMP.md)
|
||||||
|
|
||||||
[Python](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/python.md)
|
|
||||||
|
|
||||||
[LEMP Guide](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/LEMP.md)
|
|
||||||
|
|
||||||

|

|
||||||

|

|
||||||
|
|
|
||||||
Loading…
Reference in New Issue