How to parse and stringify JSON in JavaScript

JSON is the text boundary between many JavaScript programs and the data they receive from APIs, configuration files, storage, and background workers. JSON.parse() turns strict JSON text into a JavaScript value, while JSON.stringify() turns a JSON-safe value back into text for a later handoff.

Strict JSON is narrower than a JavaScript object literal. Object keys and strings use double quotes, comments and trailing commas are invalid, and a bad payload throws SyntaxError before any parsed fields exist.

Parse text at the input boundary, work with the returned object, stringify only the data that should leave the program, and keep malformed input inside a caught error path. When the output is for a log, review, or fixture, indentation from the space argument makes the string readable without changing the parsed value.

Steps to parse and stringify JSON in JavaScript:

  1. Create a script with strict JSON input, a stringified output, a round-trip check, and an invalid-input guard.
    json-round-trip.js
    const incomingJson = '{"name":"Asha","roles":["admin","editor"],"active":true}';
     
    const profile = JSON.parse(incomingJson);
    console.log(`name: ${profile.name}`);
    console.log(`second role: ${profile.roles[1]}`);
     
    const outboundJson = JSON.stringify(
      {
        name: profile.name,
        active: profile.active,
        roles: profile.roles,
      },
      null,
      2,
    );
     
    console.log("stringified:");
    console.log(outboundJson);
     
    const roundTrip = JSON.parse(outboundJson);
    console.log(`round trip active: ${roundTrip.active}`);
     
    try {
      JSON.parse("{'name':'Asha'}");
    } catch (error) {
      console.log(`malformed JSON: ${error.name}`);
    }

    Replace incomingJson with text from an API, file, or storage value. Validate pasted payloads before runtime when the source may include comments, single quotes, or trailing commas.
    Tool: JSON Validator

  2. Run the script with Node.js.
    $ node json-round-trip.js
    name: Asha
    second role: editor
    stringified:
    {
      "name": "Asha",
      "active": true,
      "roles": [
        "admin",
        "editor"
      ]
    }
    round trip active: true
    malformed JSON: SyntaxError
  3. Confirm that JSON.parse() produced object fields before using them.

    The first two output lines read profile.name and profile.roles[1] after parsing. If parsing fails, those reads never run.

  4. Confirm that JSON.stringify() returned parseable JSON text.

    The round trip active: true line comes from parsing the stringified output again. Use JSON.stringify(value, null, 2) for review-friendly indentation and JSON.stringify(value) for compact API or storage output.

  5. Convert unsupported values before stringifying them.

    JSON.stringify() throws TypeError for circular references and raw BigInt values, and it omits object properties whose values are undefined, functions, or symbols. Convert those values deliberately before sending the string to another system.

  6. Keep invalid JSON inside the error path.

    The final output line shows a caught SyntaxError from a single-quoted JSON-like string. Strict JSON needs double-quoted object keys and string values.
    Related: How to handle JavaScript errors with try-catch