Skip to content

Latest commit

 

History

History

json

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

JSON: JavaScript Object Notation

Summary

  • Human- and machine- readable and writable
  • Used for storage and transmission of data
  • Language agnostic
  • Alternative to XML in AJAX
  • JavaScript methods JSON.stringify and JSON.parse
  • Equivalents in python, Java etc

What does it look like?

  • Plain text
  • Encodes objects, lists, strings, numbers, boolean, null
  • Does not encode functions
  • Read the language summary
  • Subset of object literal notation in JavaScript
    • Object keys need to be in (double) quotes

Sending JSON with fetch

Using JSON response to update DOM (client)

JSON with express (server)

Add middleware for JSON body parsing

app.use(express.json());

Then access req.body within a .post route.

To send JSON jwith express either

Writing JSON to a file

  • Server-side only (nodejs)
  • Convert object to JSON with JSON.stringify
  • Use writeFileSync from the fs module
const fs = require('fs');
let data = JSON.stringify([1, 2, 3]);
fs.writeFileSync('./file.json', data);

Loading JSON from a file

  • Server-side only (nodejs)
  • Could load a file and then use JSON.parse
  • Or simply

const jsonContent = require("./file.json");