JSON

JavaScript Object Notation

Why JSON?

Example JSON object

  {
    "Image": {
        "Width":  800,
        "Height": 600,
        "Title":  "View from 15th Floor",
        "Thumbnail": {
            "Url":    "http://www.example.com/image/481989943",
            "Height": 125,
            "Width":  100
        },
        "Animated" : false,
        "IDs": [116, 943, 234, 38793]
      }
  }

Its Image member is an object whose Thumbnail member is an object and whose IDs member is an array of numbers.

(from the spec)

Example JSON array

[
    {
       "precision": "zip",
       "Latitude":  37.7668,
       "Longitude": -122.3959,
       "Address":   "",
       "City":      "SAN FRANCISCO",
       "State":     "CA",
       "Zip":       "94107",
       "Country":   "US"
    },
    {
       "precision": "zip",
       "Latitude":  37.371991,
       "Longitude": -122.026020,
       "Address":   "",
       "City":      "SUNNYVALE",
       "State":     "CA",
       "Zip":       "94085",
       "Country":   "US"
    }
]

(from the spec)

Viewing JSON in Browser

json viewer screenshot

Viewing JSON in NodeJS Console


$ node
> { "Image": { "Width":  800, "Height": 600, "Title":  "View from 15th Floor", "Thumbnail": { "Url":    "http://www.example.com/image/481989943", "Height": 125, "Width":  100 }, "Animated" : false, "IDs": [116, 943, 234, 38793] } }
{ Image:
   { Width: 800,
     Height: 600,
     Title: 'View from 15th Floor',
     Thumbnail:
      { Url: 'http://www.example.com/image/481989943',
        Height: 125,
        Width: 100 },
     Animated: false,
     IDs: [ 116, 943, 234, 38793 ] } }

(beware multi-line strings though: https://github.com/nodejs/node/issues/21657 )

Parsing & Producing JSON

let text = '{ "name": "Ada Lovelace", "id": 1, "title": "The Queen of Numbers" }'

The following code converts the String data into a JavaScript object:

let data = JSON.parse(text)

And this converts a JavaScript object back into a String:

let newText = JSON.stringify(data)

Using POSTMAN

https://www.getpostman.com/

 Previous Lesson Next Lesson 

Outline

[menu]

/