Course outline · 0% complete

0/29 lessons0%

Course overview →

JSON: objects as text

lesson 5-3 · ~9 min · 16/29

Why JSON exists

Objects live in your program's memory. To save one to a file or send it over the internet, you need it as text. That text format is JSON (JavaScript Object Notation), and it looks almost exactly like the object syntax you just learned. You met it in Python as the json module.

Two built-in converters, both on the JSON object:

DirectionJavaScriptPython equivalent
object → textJSON.stringify(obj)json.dumps(obj)
text → objectJSON.parse(text)json.loads(text)

JSON is stricter than JavaScript itself: keys must be double-quoted, strings must use double quotes, and there are no comments and no trailing commas. true, false, and null are lowercase, which finally matches what you learned in lesson 2-2.

Code exercise · javascript

Round trip: stringify the order into text, check that it really is a string, then parse it back and reach inside the rebuilt object.

Code exercise · javascript

Your turn. The raw string below arrived from a server. Parse it, then print the name on one line and how many languages the person knows on the next.

Quiz

Your program has an object and needs to send it over the network. Which call prepares it?