Node.js by Example: JSON

JavaScript has first class support for JSON.

JSON.strinfy() can take any JSON compatible type and turn this into a JSON string.

let json = JSON.stringify("bare string");
console.log(json);

Arrays are valid JSON types.

json = JSON.stringify([1, 2]);
console.log(json);

Only objects will begin the JSON string with curly braces.

json = JSON.stringify({ name: "John" });
console.log(json);

This supports parameters to adjust the indentation.

json = JSON.stringify({ name: "Sarah" }, 4, 4);
console.log(json);

undefined values are omitted, but null is kept as null.

json = JSON.stringify({
    name: null,
    occupation: undefined,
    title: "Engineer"
});
console.log(json);

JSON.parse() is used to parse strings of JSON into JavaScript objects.

let obj = JSON.parse(
    '{"name": "Frank", "favoriteFood": "Tacos"}'
);
console.log("obj.name", obj.name);
$ node json.js
"bare string"
[1,2]
{"name":"John"}
{
    "name": "Sarah"
}
{"name":null,"title":"Engineer"}
obj.name Frank

Next example: .