Functions and data structures · Part 9 of 36
JSON, modules and your first small program
Basic 16 min
Save data as JSON, split code into modules, and combine everything into a small, tested program.
What you will learn
- You can convert data to and from JSON in both languages.
- You can import code from modules and the standard library.
- You can plan and write a small program step by step.
Before this: Lists, dictionaries and objects
JSON: data as text
Programs need to save data and exchange it with other programs. The most common text format for that is JSON (JavaScript Object Notation, standardised in RFC 8259). It looks almost exactly like JavaScript objects and Python dictionaries:
{ "bed": "north", "plants": ["kale", "leek"], "area": 3.6, "watered": false }
Rules: keys are always in double quotes; values are strings, numbers, true/false, null, arrays or objects. No comments, no trailing commas, no functions or dates (dates travel as strings such as "2026-11-30").
const text = JSON.stringify(bed); // object → JSON text
const bed2 = JSON.parse(text); // JSON text → object
import json
text = json.dumps(bed) # dict → JSON text
bed2 = json.loads(text) # JSON text → dict
JSON.parse / json.loads throw an error on invalid text — always handle that when the text comes from outside (lesson "Errors, exceptions and debugging").

Modules: code in several files
As programs grow, you split them into modules — files that export functions and import from each other.
// garden.js
export function areaOfBed(l, w) { return l * w; }
// main.js
import { areaOfBed } from "./garden.js";
# garden.py
def area_of_bed(l, w):
return l * w
# main.py
from garden import area_of_bed
Both languages ship a standard library of ready-made modules: Python's math, json, datetime, random, pathlib; JavaScript's built-ins Math, JSON, Date (and the newer Temporal API, which started shipping in browsers in 2025), plus Node.js modules such as node:fs. Before you write something general, check whether it already exists.
Plan before you code
Here is a way to tackle any small program, for example "read a list of harvests and print the total per crop":
- Describe input and output. Input:
[{"crop": "kale", "kg": 1.2}, …]. Output: one line per crop with the total. - Work an example by hand. kale 1.2 + 0.8 = 2.0, leek 3.1.
- Write the pieces as functions. One function parses, one sums per crop, one formats.
- Test each function with your hand example.
- Connect them and run the whole thing.
def totals_by_crop(harvests):
totals = {}
for h in harvests:
totals[h["crop"]] = totals.get(h["crop"], 0) + h["kg"]
return totals
data = json.loads('[{"crop":"kale","kg":1.2},{"crop":"leek","kg":3.1},{"crop":"kale","kg":0.8}]')
for crop, kg in totals_by_crop(data).items():
print(f"{crop}: {kg:.1f} kg")
function totalsByCrop(harvests) {
const totals = {};
for (const h of harvests) totals[h.crop] = (totals[h.crop] ?? 0) + h.kg;
return totals;
}
The ?? operator in JavaScript means "use the right side if the left is null or undefined" — the same job as get(key, 0) in Python.
Try it: JavaScript sandbox
Write and run small JavaScript programs; the checker tells you when an exercise is solved.
Turn on JavaScript to use this lab. Worked example:
Exercise 9: complete totalsByCrop so that the checker's harvest list gives {"kale":2,"leek":3.1} when printed with JSON.stringify.
Where to go next
You now know the core of programming: values, variables, decisions, loops, functions and data structures. The intermediate tier shows how to organise larger programs, how to measure whether an algorithm is fast, and how professionals test and version their code.
Key points
- JSON is the standard text format for data: double-quoted keys, no comments or trailing commas.
JSON.stringify/JSON.parseandjson.dumps/json.loadsconvert data to text and back.- Modules split code into files (
export/import,from … import); use the standard library first. - Plan small programs: input/output, a hand example, small functions, tests, then connect.
??in JavaScript anddict.get(key, default)in Python give default values.
Flashcards
- Question: Which quotes must JSON keys use?
- Answer: Double quotes.
- Question: Which Python functions convert between JSON text and data?
- Answer: json.dumps (data → text) and json.loads (text → data).
- Question: What does a ?? b mean in JavaScript?
- Answer: a, unless a is null or undefined — then b.
- Question: What is the first step when planning a small program?
- Answer: Describe the input and the expected output.
Check yourself
Think about your answer, then open it. In the app the quiz is interactive, earns points and brings back what you missed.
Which of these are valid JSON?
- {"a": 1}
- {a: 1}
- [1, 2, 3]
- {'a': 1}
Show the answer
{"a": 1}; [1, 2, 3]
JSON needs double-quoted keys and strings; arrays are fine.
Source: RFC 8259
Which function turns a JSON string into a JavaScript object?
- JSON.parse
- JSON.stringify
- Object.from
- eval
Show the answer
JSON.parse
JSON.parse parses text; JSON.stringify does the reverse. Never use eval for data.
Source: MDN, JSON.parse()
What does null ?? "none" evaluate to in JavaScript?
- "none"
- null
- false
- undefined
Show the answer
"none"
The nullish coalescing operator returns the right side when the left is null or undefined.
Source: MDN, Nullish coalescing operator
Order the planning steps for a small program.
Show the answer
Describe input and output → Work an example by hand → Write small functions → Test and connect them
Understand the problem first, then build and test in small pieces.
Source: MIT OCW 6.100L
Sources
- MDN Web Docs JavaScript Guide, Mozilla, 2025
- The Python Tutorial (3.14), Python Software Foundation, 2025
- RFC 8259 The JavaScript Object Notation (JSON) Data Interchange Format, IETF, 2017
Facts checked on 2026-11-30. Skillgrove lessons are free learning material — no certificate and no accreditation.