# JSON : Basic Understanding for Beginners

# JSON: <mark>J</mark>ava<mark>S</mark>cript <mark>O</mark>bject <mark>N</mark>otation

### 1\. Manually Access Of Object :

file Name: **<mark>1_demo.html</mark>**

```xml
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Json Demo</title>
</head>

<body>
    <p>JSON : <b>J</b>ava <b>S</b>cript <b>O</b>bject <b>N</b>otation</p>
    <h1>Hello, this is Demo of JSON</h1>

    <script>        
        // 1.  Manully Access of Object : 
        const user = {
            "name": "Sandip",
            "age": 19,
            "skills": ["JAVA", "C++", "Python"],
            "add": {
                "city": "Jamnagar",
                "state": "Gujarat",
                "country": "India"
            }
        }   
        console.log(user);
    </script>
</body>
</html>
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685180742381/c0dec8cd-79f5-4ae1-9285-6252a8a6cb95.png align="center")

### 2.Fetch Single Object From .json / .js file :

```xml
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Json Demo</title>
</head>

<body>
    <p>JSON : <b>J</b>ava <b>S</b>cript <b>O</b>bject <b>N</b>otation</p>
    <h1>Hello, this is Demo of JSON</h1>

    <script>
        // 2. Fetch Single Object From .json / .js 
        fetch('1_demouser.json')
        .then(response => response.json())
        .then(json =>{
             console.log(json);
        })

    </script>
</body>

</html>
```

<mark>.json</mark> file: **<mark>1_demouser.json</mark>**

```json
{
    "name" : "Sandip",
    "age" : 19,
    "skills" : ["JAVA", "C++", "Python"],
    "add" : {   
        "city": "Jamnagar",
        "state" : "Gujarat",
        "country" : "India"
    }
}
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685180874453/01659dc0-64cf-4185-8513-7b560cb2f47b.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685180934289/ab3e1034-291f-426f-9882-4f14fe51a24e.png align="center")

### 3.Fetch Multiple Object From .json / .js file :

```xml
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Json Demo</title>
</head>

<body>
    <p>JSON : <b>J</b>ava <b>S</b>cript <b>O</b>bject <b>N</b>otation</p>
    <h1>Hello, this is Demo of JSON</h1>

    <script>
        // 3. Fetch Multiple Object From .json / .js 
        fetch('1_demouser.json')
        .then(response => response.json())
        .then(json =>{
            console.log(json);

            console.log(json.users[0]);

            console.log(json.users[1].name);
        })
    </script>
</body>
</html>
```

```json
{
    "users": [

        {
            "name": "Sandip",
            "age": 19,
            "skills": [
                "JAVA",
                "C++",
                "Python"
            ],
            "add": {
                "city": "Jamnagar",
                "state": "Gujarat",
                "country": "India"
            }
        }, 

        {
            "name": "Jay",
            "age": 20,
            "skills": [
                "C",
                "C++",
                "Python"
            ],
            "add": {
                "city": "Rajkot",
                "state": "Gujarat",
                "country": "India"
            }
        },

        {
            "name": "Hardik",
            "age": 21,
            "skills": [
                "PHP",
                "C++",
                "Django"
            ],
            "add": {
                "city": "Delhi",
                "state": "Delhi",
                "country": "India"
            }
            
        }


    ]
}
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685181890941/232b3e01-a14f-45dc-9c8b-d6e708b9b36e.png align="center")

<mark>Show on the webpage :</mark>

```xml
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Json Demo</title>
</head>

<body>
    <p>JSON : <b>J</b>ava <b>S</b>cript <b>O</b>bject <b>N</b>otation</p>
    <h1>Hello, </h1>


    <ol></ol>

    <script>
        // 3. Fetch Multiple Object From .json / .js 

        let list = ""
        fetch('1_demouser.json')
        .then(response => response.json())
        .then(json =>{
            

            json.users.forEach(user => {
                list += `<li>${user.name}</li>`
            });
            document.querySelector('ol').innerHTML = list;
        })

    </script>
</body>
</html>
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685182875435/0b5350ef-87c5-4326-be83-07df161efa98.png align="center")

## <mark>Object</mark> (Client Side) to <mark>.json String</mark> (Server Side) &

## <mark>.json String</mark> (Server Side) to <mark>Object</mark> (Client Side) :

```xml
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Json Demo</title>
</head>

<body>
    
    <script>

        const users = [

            {
                "name": "Sandip",
                "age": 19,
                "skills": ["JAVA", "C++", "Python"],
                "add": {
                    "city": "Jamnagar",
                    "state": "Gujarat",
                    "country": "India"
                }
            },

            {
                "name": "Jay",
                "age": 20,
                "skills": [
                    "C",
                    "C++",
                    "Python"
                ],
                "add": {
                    "city": "Rajkot",
                    "state": "Gujarat",
                    "country": "India"
                }
            }
        ]

        // Object to String
        let userString = JSON.stringify(users);
        console.log(userString);

        // String to Object
        console.log(JSON.parse(userString));
        
    </script>
</body>

</html>
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685183683981/00feb0f7-b18b-4622-9e84-2bd8b527423a.png align="center")

## Free Fake API for testing :

## Visit : { [<mark>Json PlaceHolder</mark>](https://jsonplaceholder.typicode.com/) }

```xml
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Json Demo</title>
</head>
<body>
    <h1>Todo List</h1>
    <ol></ol>
    <script>

        let list = ""
        fetch('https://jsonplaceholder.typicode.com/todos/')
        .then(response => response.json())
        .then(json =>{
            
            json.forEach(todo => {
                list += `<li>${todo.title}</li>`
            });
            document.querySelector('ol').innerHTML = list;
        })

    </script>
</body>
</html>
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685184878964/6345292a-7cd2-4d04-bcf7-5beaa8464544.png align="center")
