JSON Blog
JSON stands for JavaScript Object Notation
JSON is a text and file format used for storing and transfering data. It is widely used and supported across multiple programming platforms. The syntax of a JSON string is very similar to a javascript object.
Below is an example of converting a JavaScript object to a JSON string.
const cats = [
{id: 1, name: "Felix", birthdate:"2022-02-22", vaccinated: true, owner:"Bob Smith"},
{id: 2, name: "Garfield", birthdate:"2019-10-23", vaccinated: false, owner:"Bob Smith"},
{id: 3, name: "Milo", birthdate:"2020-04-11", vaccinated: true, owner:"Patty Jones"}
];
const JsonCats = JSON.stringify(cats);
console.log(JsonCats);
//The JsonCats variable will look like this:
[{"id":1,"name":"Felix","birthdate":"2022-02-22","vaccinated":true,"owner":"Bob Smith"},
{"id":2,"name":"Garfield","birthdate":"2019-10-23","vaccinated":false,"owner":"Bob Smith"},
{"id":3,"name":"Milo","birthdate":"2020-04-11","vaccinated":true,"owner":"Patty Jones"}]
Additionally, Here is an example of converting from a JSON string to a JavaScript object
const JsonCats = [{"id":1,"name":"Felix","birthdate":"2022-02-22","vaccinated":true,"owner":"Bob Smith"},
{"id":2,"name":"Garfield","birthdate":"2019-10-23","vaccinated":false,"owner":"Bob Smith"},
{"id":3,"name":"Milo","birthdate":"2020-04-11","vaccinated":true,"owner":"Patty Jones"}]
const regularCats = JSON.parse(JsonCats);
console.log(regularCats);
//The regularCats variable will look like this:
{id: 1, name: "Felix", birthdate:"2022-02-22", vaccinated: true, owner:"Bob Smith"},
{id: 2, name: "Garfield", birthdate:"2019-10-23", vaccinated: false, owner:"Bob Smith"},
{id: 3, name: "Milo", birthdate:"2020-04-11", vaccinated: true, owner:"Patty Jones"}
You may notice that JSON and JavaScript objects look very similar. However, there are some key differences between the 2 formats. For example, key names are put into double quotations in JSON format, and a property can have an array of values instead of one single value.
Data in JSON string file format has a lot of versatility as it can be pretty easily transformed into another data format. JSON format is also primarily used for transmitting data between a server and a web application, which is why the parse and stringify commands in javascript are so useful.