JavaScript Examples: to and from JSON string

let example = {
intField: 1,
strField: "hello",
boolField: false
};

console.log("default javascript representation");
console.log(example);

// to json
let exampleJson = JSON.stringify(example, null, 2);
console.log("json:");
console.log(exampleJson);

// load json
let parsedObj = JSON.parse(exampleJson);
if (parsedObj['intField'] !== example['intField']) {
throw new Error("Assertion failed");
}

exampleJson;

Result

Console output