|
| 1 | +# Working with JSON in JavaScript |
| 2 | +This project is a basic demonstration of how to use **JSON (JavaScript Object Notation)** in JavaScript. I used this to learn how to: |
| 3 | +- Convert JavaScript data (arrays and objects) to JSON strings using *JSON.stringify()* |
| 4 | +- Convert JSON strings back into JavaScript data using *JSON.parse()* |
| 5 | +- Fetch JSON data from a file using *fetch()* |
| 6 | +- SSave and retrieve JSON data using the browser's *local storage* |
| 7 | + |
| 8 | +## I demostrated my understanding as illustrated in the examples below |
| 9 | +### Converting JavaScript to JSON String using *JSON.stringify()* |
| 10 | +I converted an array of names to a JSON string using *JSON.stringify()*. This is useful for saving or sending the data. |
| 11 | + |
| 12 | +#### Example 1: Array to JSON string |
| 13 | +```javascript |
| 14 | +const names = ["Linus", "Melvin", "Sandra", "Laura"]; |
| 15 | +const namesString = JSON.stringify(names); |
| 16 | +console.log(namesString); |
| 17 | +``` |
| 18 | + |
| 19 | +#### Console Output |
| 20 | +```javascript |
| 21 | +["Linus","Melvin","Sandra","Laura"] |
| 22 | +``` |
| 23 | + |
| 24 | +#### Example 2: Object to JSON string |
| 25 | +I converted a single person object into a JSON string. |
| 26 | +```javascript |
| 27 | +const person = { |
| 28 | + "name": "Linus", |
| 29 | + "age": 29, |
| 30 | + "isEmployed": false |
| 31 | +} |
| 32 | +const personString = JSON.stringify(person); |
| 33 | +console.log(personString); |
| 34 | +``` |
| 35 | + |
| 36 | +#### Console Output |
| 37 | +```javascript |
| 38 | +{"name":"Linus","age":29,"isEmployed":false} |
| 39 | +``` |
| 40 | + |
| 41 | +#### Example 3: Array of objects to JSON string |
| 42 | +I converted an array of multiple people objects into a JSON string. |
| 43 | +```javascript |
| 44 | +const people = [ |
| 45 | + { "name": "Linus", "age": 29, "isEmployed": false }, |
| 46 | + { "name": "Laura", "age": 25, "isEmployed": false }, |
| 47 | + { "name": "Lawrence", "age": 35, "isEmployed": false }, |
| 48 | + { "name": "Sandra", "age": 19, "isEmployed": false } |
| 49 | +]; |
| 50 | + |
| 51 | +const peopleString = JSON.stringify(people); |
| 52 | +console.log(peopleString); |
| 53 | +``` |
| 54 | + |
| 55 | +#### Console Output |
| 56 | +```javascript |
| 57 | +[{"name":"Linus","age":29,"isEmployed":false},{"name":"Laura","age":25,"isEmployed":false},{"name":"Lawrence","age":35,"isEmployed":false},{"name":"Sandra","age":19,"isEmployed":false}] |
| 58 | +``` |
| 59 | + |
| 60 | +### Converting JSON String to JavaScript using *JSON.parse()* |
| 61 | +#### Example 1: JSON string (array) to JavaScript array |
| 62 | +I turned a JSON string back into a normal JavaScript array using *JSON.parse()*. |
| 63 | +```javascript |
| 64 | +const jsonNames = `["Linus", "Melvin", "Sandra", "Laura"]`; |
| 65 | +const parsedNames = JSON.parse(jsonNames); |
| 66 | +console.log(parsedNames); |
| 67 | +Console Output: |
| 68 | +``` |
| 69 | + |
| 70 | +#### Console Output |
| 71 | +```javascript |
| 72 | +[ 'Linus', 'Melvin', 'Sandra', 'Laura' ] |
| 73 | +``` |
| 74 | + |
| 75 | +#### Example 2: JSON string (object) to JavaScript object |
| 76 | +I turned a JSON string into a regular JavaScript object. |
| 77 | +```javascript |
| 78 | +const jsonPerson = `{ |
| 79 | + "name": "Linus", |
| 80 | + "age": 29, |
| 81 | + "isEmployed": false |
| 82 | +}`; |
| 83 | +const parsedPerson = JSON.parse(jsonPerson); |
| 84 | +console.log(parsedPerson); |
| 85 | +``` |
| 86 | + |
| 87 | +#### Console Output |
| 88 | +```javascript |
| 89 | +{ name: 'Linus', age: 29, isEmployed: false } |
| 90 | +``` |
| 91 | + |
| 92 | +#### Example 3: JSON string (array of objects) to JavaScript array |
| 93 | +I converted a JSON string of people into a usable JavaScript array of objects. |
| 94 | + |
| 95 | +```javascript |
| 96 | +const jsonPeople = `[{ |
| 97 | + "name": "Linus", |
| 98 | + "age": 29, |
| 99 | + "isEmployed": false |
| 100 | +}, |
| 101 | +{ |
| 102 | + "name": "Laura", |
| 103 | + "age": 25, |
| 104 | + "isEmployed": false |
| 105 | +}, |
| 106 | +{ |
| 107 | + "name": "Lawrence", |
| 108 | + "age": 35, |
| 109 | + "isEmployed": false |
| 110 | +}, |
| 111 | +{ |
| 112 | + "name": "Sandra", |
| 113 | + "age": 19, |
| 114 | + "isEmployed": false |
| 115 | +}]`; |
| 116 | +const parsedPeople = JSON.parse(jsonPeople); |
| 117 | +console.log(parsedPeople); |
| 118 | +``` |
| 119 | + |
| 120 | +#### Console Output: |
| 121 | +```javascript |
| 122 | +[ |
| 123 | + { name: 'Linus', age: 29, isEmployed: false }, |
| 124 | + { name: 'Laura', age: 25, isEmployed: false }, |
| 125 | + { name: 'Lawrence', age: 35, isEmployed: false }, |
| 126 | + { name: 'Sandra', age: 19, isEmployed: false } |
| 127 | +] |
| 128 | +``` |
| 129 | + |
| 130 | +### Fetching JSON Data using fetch() |
| 131 | +I fetched a JSON file and displayed each object using *.forEach()*. I learnt how *fetch()* returns a promise and how *.then()* works |
| 132 | +```javascript |
| 133 | +fetch("people.JSON") |
| 134 | + .then(response => response.json()) |
| 135 | + .then(value => value.forEach((person) => { |
| 136 | + console.log(person); |
| 137 | + })); |
| 138 | +``` |
| 139 | + |
| 140 | +The *people.JSON* file contained: |
| 141 | +```javascript |
| 142 | +[ |
| 143 | + { "name": "Linus", "age": 29, "isEmployed": false }, |
| 144 | + { "name": "Laura", "age": 25, "isEmployed": false }, |
| 145 | + { "name": "Lawrence", "age": 35, "isEmployed": false }, |
| 146 | + { "name": "Sandra", "age": 19, "isEmployed": false } |
| 147 | +] |
| 148 | +``` |
| 149 | + |
| 150 | +#### Console Output: |
| 151 | +```javascript |
| 152 | +{ name: 'Linus', age: 29, isEmployed: false } |
| 153 | +{ name: 'Laura', age: 25, isEmployed: false } |
| 154 | +{ name: 'Lawrence', age: 35, isEmployed: false } |
| 155 | +{ name: 'Sandra', age: 19, isEmployed: false } |
| 156 | +``` |
| 157 | + |
| 158 | +### Saving and Retrieving JSON from Local Storage |
| 159 | +#### Saving to Local Storage |
| 160 | +I converted a car array to a JSON string and saved it to local storage with the key "*carsToJSON*". |
| 161 | +```javascript |
| 162 | +const cars = ["mazda", "BMW", "Auris", "Volvo"]; |
| 163 | +const carsToJSON = JSON.stringify(cars); |
| 164 | +localStorage.setItem("carsToJSON", carsToJSON); |
| 165 | +``` |
| 166 | + |
| 167 | +#### Retrieving from Local Storage |
| 168 | +I retrieved the JSON string from local storage and converted it back into a normal JavaScript array. |
| 169 | +```javascript |
| 170 | +const savedCars = localStorage.getItem("carsToJSON"); |
| 171 | +const JSONToCars = JSON.parse(savedCars); |
| 172 | +console.log(JSONToCars); |
| 173 | +``` |
| 174 | + |
| 175 | +#### Console Output: |
| 176 | +```javascript |
| 177 | +[ 'mazda', 'BMW', 'Auris', 'Volvo' ] |
| 178 | +``` |
| 179 | + |
| 180 | +## What I learnt |
| 181 | +- How to convert JavaScript data to JSON using *JSON.stringify()* |
| 182 | +- How to convert JSON strings back to JavaScript using *JSON.parse()* |
| 183 | +- How to use *fetch()* to get JSON data from a file |
| 184 | +- How to store and retrieve JSON data in the browser's local storage |
0 commit comments