Parsing and Stringifying JSON
Since JSON is just text, JavaScript needs two built-in functions to move between "JSON as a string" and "JSON as real JavaScript values": JSON.parse and JSON.stringify.
JavaScript
Reading it
JSON.parse(text)turns a JSON string into real JavaScript values (objects, arrays, numbers, ...), throws aSyntaxErrorif the text isn't valid JSON.JSON.stringify(value)turns a JavaScript value into a JSON string.- The third argument to
stringify, here2, adds indentation, handy for logging or writing readable files, it has no effect on parsing. - Anything
JSON.stringifycan't represent, functions,undefinedvalues, gets silently dropped from objects (or turned intonullinside arrays).
JavaScript