JSON.parse()

JSON.parse()

  • When JSON is received from another service, it is in a string format

  • In order to effectively work with JSON data in our code, we need to covert it so an object, array or value

  • JSON.parse() is a method that converts an stringified JSON to native JavaScript datatypes (most commonly objects or arrays)


Example

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

const parsedJson = JSON.parse(jsonString)

console.log('parsedJson', parsedJson)

console.log(`data type of parsedJson: ${typeof parsedJson}`)

JS Bin on jsbin.com