JSON.stringify()

JSON.stringify()

  • When JSON is used to send data from one service to the next “over the wire” (i.e. via cloud) it is transmitted as a string which makes it lightweight and fast

  • JSON.stringify() allows developers to easily transform objects / arrays (or any value) into a JSON string which makes it compatible to be transmitted along with an HTTP Request

  • “Stringify-ing” data is a common step developers take before sending data to another service


Example

  const avengers = {
    members: [
      {
        name: "Spider-Man",
        alias: "Peter Parker"
      },
      {
        name: "Black Widow",
        alias: "Natasha Romanova"
      }
    ]
  }

  // convert object into a JSON string
  const jsonString = JSON.stringify(avengers))
  console.log(jsonString)

  console.log(typeof jsonString); // result: string

JS Bin on jsbin.com