Updated November 24, 2026 | Developer Guides | Search-focused practical tutorial

Two built-in JavaScript methods cover many everyday JSON tasks: JSON.parse and JSON.stringify. Parsing turns a string that contains JSON into a JavaScript value. Stringifying converts a supported JavaScript value into JSON text. Confusing these directions causes errors in fetch handlers, browser storage and APIs.

\n

Illustration of JSON parsing and stringification

\n

Quick takeaway: This guide explains json.parse vs json.stringify through real-world examples, frequent errors and ways to verify your work. Use sample records rather than private client data when trying online tools.

JSON.parse turns text into data

JSON.parse accepts valid JSON text and returns a JavaScript value. The result can be an object, array, string, number, boolean or null. If the text contains unquoted keys, trailing commas or broken escaping, parsing throws a SyntaxError.

const text = '{"city":"Toronto","visitors":23}';
const data = JSON.parse(text);
console.log(data.city); // Toronto

When response.json has already parsed an HTTP response, the result is typically ready to use; do not pass an existing object to JSON.parse expecting another conversion.

JSON.stringify turns data into text

JSON.stringify serializes supported JavaScript values into a JSON string. Use it to create request bodies or inspect data in a stable format. The optional space argument can create readable indentation. Whitespace differences do not change valid JSON’s represented value.

const data = { city:"Sydney", active:true };
console.log(JSON.stringify(data));
console.log(JSON.stringify(data, null, 2));

The output is a string, not an automatically saved file. A separate operation writes it to storage or sends it to a server.

Catch malformed JSON without crashing a page

Wrap parsing of untrusted text in try/catch and give users a useful explanation when their sample is invalid. Include a line or column if your tooling can provide it. Be cautious about logging the entire input because it could contain private information.

function safeParse(value) {
  try { return {ok:true, data:JSON.parse(value)}; }
  catch { return {ok:false, error:"Invalid JSON text"}; }
}

An explicit result shape is often easier for callers to handle than returning null for every failure, because null could also be a legitimate JSON value.

Serialization changes some JavaScript values

JSON.stringify omits object properties whose values are undefined or functions, while undefined entries in arrays become null. BigInt raises an error unless custom handling is provided. Dates often serialize to date-time strings, and circular references fail without special processing.

JSON.stringify({a:undefined,b:1}); // {"b":1}
JSON.stringify([undefined,1]); // [null,1]

Therefore, JSON.parse(JSON.stringify(value)) is not a universal deep clone. It can discard unsupported information and change certain types.

Nested data, validation and security

After parsing, confirm the returned object follows your application’s requirements before reading deeply nested fields. A JSON formatter makes samples easier to inspect, but it cannot guarantee that customerId exists, a price is positive or an account is authorized. Treat untrusted JSON as untrusted data even when syntax is valid.When displaying values from parsed JSON in HTML, use safe DOM text APIs instead of inserting them as raw markup. When sending JSON to an API, follow its authentication, CORS and validation requirements.

Frequently asked questions

What does JSON.parse return? A JavaScript value. Does JSON.stringify fetch an API? No, it only creates text. Will stringify handle a cyclic object? Not by default. Can JSON store a function? JSON has no function type. Can JavaScript parse Chinese strings? Yes, with correctly encoded JSON. Is JSON identical to JavaScript object syntax? No, JSON is stricter about quotes and supported values.

Practical checklist before using this in production

  • Confirm that the source, file extension, expected type and encoding match your data contract.
  • Test a typical valid example and at least one deliberately invalid example.
  • Check empty values, missing fields, Unicode input and unusual numbers rather than relying only on the easiest sample.
  • Keep secrets and personal data out of public web-based tools unless your organization has approved the handling.
  • Document version, timezone and currency assumptions for projects in the USA, UK, Canada, Australia or China where they are relevant.

These practices make the examples easier to adapt internationally without assuming that a local service is available in every region.

Relevant free tools on Perkability Rewards

Try the directly linked utilities, not a new landing page. The site also has an existing JSON errors guide and a JSON arrays tutorial.

  • JSON Formatter: practise using non-sensitive sample data relevant to this topic.
  • HTML Entity Encoder: practise using non-sensitive sample data relevant to this topic.

Authoritative technical references:

Disclosure: This educational article includes a third-party Adsterra 160×600 banner and social bar. Their display depends on the visitor’s browser and advertising-network settings. The article does not make promises about Google ranking or location-specific availability.

2 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *