Skip to content

Visualising Data with Javascript

Visualising data with JavaScript

Image by rawpixel.com on Freepik

JSON vs JavaScript object

JSON vs JavaScript object

JSON.parse()

Example:

js
const text = '{"name":"John", "birth":"1986-12-14", "city":"New York"}';
const obj = JSON.parse(text);
//console.log(obj.birth);
obj.birth = new Date(obj.birth);
//console.log(obj.birth);

document.getElementById("demo").innerHTML = obj.name + ", " + obj.birth;
const text = '{"name":"John", "birth":"1986-12-14", "city":"New York"}';
const obj = JSON.parse(text);
//console.log(obj.birth);
obj.birth = new Date(obj.birth);
//console.log(obj.birth);

document.getElementById("demo").innerHTML = obj.name + ", " + obj.birth;

JSON.stringify()

Example:

js

const obj = {name: "John", age: 30, city: "New York"};
//console.log(obj);
const myJSON = JSON.stringify(obj);
//console.log(myJSON);

document.getElementById("demo").innerHTML = "Here is my JSON: " + myJSON;

const obj = {name: "John", age: 30, city: "New York"};
//console.log(obj);
const myJSON = JSON.stringify(obj);
//console.log(myJSON);

document.getElementById("demo").innerHTML = "Here is my JSON: " + myJSON;

JSON references and tools

Free to use JSON datasets: https://github.com/jdorfman/awesome-json-datasets.

Use https://jsonbeautify.com/ to look at complicated JSONs and format them so they are understandable.

References

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON
https://www.w3schools.com/js/js_json_parse.asp
https://www.w3schools.com/js/js_json_stringify.asp
https://www.w3schools.com/js/js_json_objects.asp
https://www.w3schools.com/js/js_json_arrays.asp
https://github.com/jdorfman/awesome-json-datasets
https://jsonbeautify.com/

Content CC BY 4.0 | Code AGPL 3.0