Lesson 4 of 6

Parsing & Stringifying

JSON is just text — a string of characters. Before your program can actually use it, that text has to become a real in-memory object your language understands. That conversion, in both directions, is what parsing and stringifying mean, and every mainstream language ships a built-in way to do both.

💡 The Bottom Line

Parsing turns a JSON string into a native object/array/value you can work with in code. Stringifying (also called serializing) does the reverse — turns a native object back into JSON text, ready to send over a network or save to a file.

In JavaScript

const jsonText = '{"name": "Ada", "age": 36, "active": true}';

// Parse: JSON text -> real JavaScript object
const person = JSON.parse(jsonText);
console.log(person.name); // "Ada"
console.log(typeof person); // "object"

// Stringify: JavaScript object -> JSON text
const backToText = JSON.stringify(person);
console.log(backToText); // '{"name":"Ada","age":36,"active":true}'

// Pretty-print with indentation (useful for logs/debugging)
console.log(JSON.stringify(person, null, 2));

Note that JSON.parse throws a real error if the text isn't valid JSON — it doesn't silently return something wrong. Always expect to handle that error in real code (a try/catch around the parse call), especially when the JSON is coming from an external source like a network request.

The same idea in Python

import json

json_text = '{"name": "Ada", "age": 36, "active": true}'

# Parse: JSON text -> Python dict
person = json.loads(json_text)
print(person["name"])  # "Ada"

# Stringify: Python dict -> JSON text
back_to_text = json.dumps(person)
print(back_to_text)

# Pretty-print
print(json.dumps(person, indent=2))

Notice the naming difference: JavaScript uses parse/stringify, Python uses loads/dumps ("load string" / "dump string"). Different names, identical concept — every language has this exact pair of operations somewhere in its standard library.

⚠️ Watch Out

Stringifying doesn't always round-trip perfectly. Some values your language supports simply don't exist in JSON — JavaScript's undefined, functions, and Date objects don't have a direct JSON equivalent. JSON.stringify silently drops undefined values and functions, and converts Date objects to an ISO string automatically — which then needs to be manually parsed back into a real Date after the next JSON.parse, since JSON has no date type at all (see Lesson 1).

🙋 There Are No Dumb Questions

Q: Why would I ever need to stringify manually — doesn't my HTTP library handle this automatically?
A: Often yes — most modern HTTP client/server libraries stringify a request/response body for you automatically when you set the right content type. But you'll still reach for manual stringify/parse constantly: saving data to localStorage (which only stores strings), logging an object in a readable way, writing to a file, or debugging exactly what bytes are about to go over the wire.

🔧 Try It Yourself

In a browser console, create an object with a nested array and object inside it. Run JSON.stringify(yourObject, null, 2) and look at the pretty-printed output. Then copy that output, and paste it into JSON.parse("...") to confirm you get an identical object back — a live demonstration of the round trip.

✅ Quick Check

What happens if you call JSON.parse() on a string that isn't valid JSON?


Next up: how to make sure the JSON you're receiving actually has the shape you expect, before your code tries to use it — JSON Schema and validation.

← Back to
Next →