JSON
What is JSON
JSON stands for JavaScript Object Notation
JSON is a text format for storing and transporting data
JSON is "self-describing" and easy to understand
JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page)
JSON Example
'{"name":"John", "age":30, "money": 5000}'
How to access the data from JSON?
Let's see the example ...
- In the Visual Studio Code, create an
data.json
file -
copy this code to your data
.json
file{ "users": [ { "id": 1, "firstname": "Alice", "balance": 1000, "credit_ID": 2 }, { "id": 2, "firstname": "Bob", "balance": 500, "credit_ID": 1 }, { "id": 3, "firstname": "Charlie", "balance": 1500, "credit_ID": 4 }, { "id": 4, "firstname": "David", "balance": 2000, "credit_ID": 3 } ], "banks": [ { "id": 1, "owner": 1, "sender": "Alice", "receiver": 2, "note": "For lunch", "amount": 200, "bank": "Bank A", "type": "transfer" }, { "id": 2, "owner": 2, "sender": "Bob", "receiver": 1, "note": "Repayment", "amount": 100, "bank": "Bank B", "type": "transfer" }, { "id": 3, "owner": 3, "sender": "Charlie", "receiver": 4, "note": "Gift", "amount": 500, "bank": "Bank C", "type": "transfer" }, { "id": 4, "owner": 1, "sender": "Alice", "receiver": 3, "note": "For lunch", "amount": 500, "bank": "Bank A", "type": "transfer" } ] }
- Create
index.js
file
const data = require("./data.json");
console.log(data.banks[0]);
- Query in JSON File
orconst data = require("./data.json"); data.users.forEach((e) => { if (e.firstname == "Bob") { console.log(e); } });
const data = require("./data.json"); console.log(data.users.find((user) => user.firstname === "Bob"));
--- End of the example ---