# JSON

Java Script Object Notation.

This is neither a row-based nor Columnar Format.

The flexible way to store & share data across systems.

It's a text file with curly braces & key-value pairs { }

Simplest JSON format

```
{"id": "1","name":"Rachel"}
```

### Properties

* Language Independent.
* Self-describing and easy to understand.

### Basic Rules

* Curly braces to hold the objects.
* Data is represented in Key Value or Name Value pairs.
* Data is separated by a comma.
* The use of double quotes is necessary.
* Square brackets \[ ] hold an array of data.

### JSON Values <a href="#json-values" id="json-values"></a>

```
String  {"name":"Rachel"}

Number  {"id":101}

Boolean {"result":true, "status":false}  (lowercase)

Object  {
            "character":{"fname":"Rachel","lname":"Green"}
        }

Array   {
            "characters":["Rachel","Ross","Joey","Chanlder"]
        }

NULL    {"id":null}
```

### Sample JSON Document

```
{
    "characters": [
        {
            "id" : 1,
            "fName":"Rachel",
            "lName":"Green",
            "status":true
        },
        {
            "id" : 2,
            "fName":"Ross",
            "lName":"Geller",
            "status":true
        },
        {
            "id" : 3,
            "fName":"Chandler",
            "lName":"Bing",
            "status":true
        },
        {
            "id" : 4,
            "fName":"Phebe",
            "lName":"Buffay",
            "status":false
        }
    ]
}
```

### JSON Best Practices <a href="#json-best-practices" id="json-best-practices"></a>

**No Hyphen in your Keys.**

```
{"first-name":"Rachel","last-name":"Green"}  is not right. ✘
```

**Under Scores Okay**

```
{"first_name":"Rachel","last_name":"Green"} is okay ✓
```

L**owercase Okay**

```
{"firstname":"Rachel","lastname":"Green"} is okay ✓
```

**Camelcase best**

```
{"firstName":"Rachel","lastName":"Green"} is the best. ✓
```

### **Use Cases**:

* **APIs and Web Services**: JSON is widely used in RESTful APIs for sending and receiving data.
* **Configuration Files**: Many modern applications and development tools use JSON for configuration.
* **Data Storage**: Some NoSQL databases like MongoDB use JSON or BSON (binary JSON) for storing data.
* **Serialization and Deserialization**: Converting data to/from a format that can be stored or transmitted.

### Serialize

Convert Python Object to JSON Format

```python
import json

# Python dictionary with Friend's characters
friends_characters = {
    "characters": [
        {"name": "Rachel Green", "job": "Fashion Executive"},
        {"name": "Ross Geller", "job": "Paleontologist"},
        {"name": "Monica Geller", "job": "Chef"},
        {"name": "Chandler Bing", "job": "Statistical Analysis and Data Reconfiguration"},
        {"name": "Joey Tribbiani", "job": "Actor"},
        {"name": "Phoebe Buffay", "job": "Massage Therapist"}
    ]
}

print(type(friends_characters), friends_characters)

print("-" * 200 )

# Serializing json
json_data = json.dumps(friends_characters, indent=4)
print(type(json_data), json_data)

# Saving to a file
with open('friends_characters.json', 'w') as file:
    json.dump(friends_characters, file, indent=4)
```

**DeSerialize**&#x20;

Convert JSON Format/File to Python Object

```python
import json

# JSON string with Friend's characters
json_data = '''
{
    "characters": [
        {"name": "Rachel Green", "job": "Fashion Executive"},
        {"name": "Ross Geller", "job": "Paleontologist"},
        {"name": "Monica Geller", "job": "Chef"},
        {"name": "Chandler Bing", "job": "Statistical Analysis and Data Reconfiguration"},
        {"name": "Joey Tribbiani", "job": "Actor"},
        {"name": "Phoebe Buffay", "job": "Massage Therapist"}
    ]
}
'''

# Parsing JSON to dictionary
friends_characters = json.loads(json_data)
print(friends_characters, type(friends_characters))

## Alternate way to read from file

# Path to your JSON file
file_path = 'friends_characters.json'

# Open the file and read the JSON content
with open(file_path, 'r') as file:
    data = json.load(file)
print(data, type(data))
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gchandra.gitbook.io/big-data-and-tools-with-nosql/data-format/json.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
