Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sujon-ahmed/intro-to-json


https://github.com/sujon-ahmed/intro-to-json

Last synced: about 5 hours ago
JSON representation

Awesome Lists containing this project

README

        

# Intro to JSON

## What is JSON?
JSON (Javascript Object Notation) is a lightweight data interchange format. It uses key-value pairs and is similar to object literals in JavaScript.

The basic syntax consists of curly braces {}

for objects and square brackets [] for arrays.

## Create JSON Objects/Arrays
You can create JSON objects and arrays in JavaScript using their respective literal syntax.

```js
// JSON Object
const person = {
name: "Sujon Ahmed",
age: 24,
email: "[email protected]"
};

// JSON Array
const fruits = ["Apple", "Banana", "Orange", "Mango"];
```

## Parse JSON
To convert a JSON string into a Javascript object, you can use JSON.parse().

```js
const jsonString = '{"name": "Sujon Ahmed", "age": 24}';

const person = JSON.parse(jsonString);

console.log(person.name); // Output: Sujon Ahmed
```

## Stringify JS objects to JSON
To convert a JavaScript Object into a JSON string, you can use JSON.stringify().

```js
const person = {
name: "Sujon Ahmed"
age: 24
};

const jsonString = JSON.stringify(person);

console.log(jsonString);
// Output: '{"name": "Sujon Ahmed", "age": 24}'
```

## Nested JSON
JSON supports nested structures, allowing you to create complex data hierarchies.

```js
{
"name": "Sujon Ahmed",
"age": 24,
"address": {
"city": "Dhaka City",
"zipCode": "1207"
}
}
```

## Working with JSON Arrays
JSON array can contain multiple values of different types, including other objects or arrays.

```js
[
{
"name": "Sujon Ahmed",
"age": 24
},
{
"name": "Riman",
"age": 25
}
]
```