Using JSON format

Monday, Aug 3, 2020
Frontend JavaScript

It turns out that it is common for APIs to output data in JSON format. JSON(JavaScript Object Notation) format is surprisingly simple - it's basically a stringified version of regular JavaScript object, meaning if we have an object {name: "param", hasPlayed: true}, it's JSON equivalent is '{"name": "param","hasPlayed": false}'. We use JSON.stringify to convert object to JSON and JSON.parse to get back the object from JSON:

Converting JavaScript object to JSON

let player = {
    name: "param",
    hasPlayed: true

let outData = JSON.stringify(player);

Converting JSON to JavaScript object

let inData = JSON.parse(outData);