Go · Chapter 12 of 26
JSON
By the end of this chapter you can turn Go structs into JSON and JSON back into structs with encoding/json, choose each field's JSON name with a struct tag, leave out empty fields, refuse messages with fields you do not know, read times and lists, and say why BijliGo's JSON carries money as an int of paise. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Real programs
- 4 tasks
- Go
- In the all labs plan
The first 2 chapters of Go are free. This one comes with the all labs plan.
Why you need this
Everything that talks to BijliGo talks in JSON. Every charger sends its meter message as a small JSON object every 10 seconds while it charges. The app asks the status API about a charger and gets JSON back. The file of Wednesday's 3,600 sessions, /workspace/bijli/data/sessions.json, is one long JSON list. And Pooja's old billing system sends refunds as JSON too, with rupees written as decimals, which is where her ledger and Tenzin's script disagree by a paisa.
So far your Go has printed text with fmt and kept values in structs. You could build a JSON object by hand with fmt.Sprintf, quotes and commas, and it would break the first time a station's name had a quote in it. And reading JSON by hand, finding each name and cutting out its value, is far worse. Go's standard library does both, carefully, in one package.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
status.go: a charger's status as JSON
Give each field of StatusMessage a json tag and write EncodeStatus(s StatusMessage) ([]byte, error), which returns the status as compact JSON on one line. The JSON names are charger_id (Charger), station_id (Station), state (State), power_kw (PowerKW), fault (Fault, left out entirely when it is empty) and seen_at (SeenAt), in that order. Keep the field names and types as they are.
For C1042 at ST007, available, 30 kW, no fault, seen at 7 pm IST on 14 October 2026, it returns exactly:
{"charger_id":"C1042","station_id":"ST007","state":"available","power_kw":30,"seen_at":"2026-10-14T19:00:00+05:30"}
The Check also tries C1057 at ST010, faulted, 3.3 kW, with the fault overcurrent, seen at 19:12:30, and expects:
{"charger_id":"C1057","station_id":"ST010","state":"faulted","power_kw":3.3,"fault":"overcurrent","seen_at":"2026-10-14T19:12:30+05:30"}
- Task 2
meter.go: read a meter message, and refuse a bad one
Write DecodeMeter(data []byte) (MeterMessage, error). For a good message such as {"charger":"C1003","session":"S900539","meter_wh":4120,"power_w":28950} it returns MeterMessage{Charger: "C1003", Session: "S900539", MeterWh: 4120, PowerW: 28950} and a nil error; a meter_wh of 0 is good too.
It returns an error that wraps ErrBadMeter (the Check uses errors.Is(err, ErrBadMeter)) when the message is not JSON, when a value has the wrong type (meter_wh as the text "4120"), when it has a field MeterMessage does not have (the Check adds "volts":400), when the charger or the session is missing, and when meter_wh is below 0.
- Task 3
sessions.go: read BijliGo's file of sessions
Give each field of SessionRecord the json tag of its name in BijliGo's sessions file (id, charger, started_at, energy_wh, cost_paise, status), and write ParseSessions(data []byte) ([]SessionRecord, error), which reads a JSON list of sessions and returns them in the list's order. Other fields in the file (station, driver, stopped_at, paise_per_kwh) are skipped. Keep the field names and types as they are.
For the real file (/workspace/bijli/data/sessions.json) the Check expects 3600 sessions, the first S901061 on C1379, completed, started at 2026-10-14T06:00:00+05:30, with 3631 Wh and 5083 paise, and all of them adding up to 44632024 Wh and 83158805 paise. It also tries a list of one session.
ParseSessions returns an error when the JSON is broken, when it is one object instead of a list, and when a value has the wrong type (energy_wh as text).
- Task 4 · interview question
Interview: refunds.go, the refund total a paisa short
BijliGo's old billing system sends refunds as a JSON list with rupees as decimal numbers: [{"session":"S900276","rupees":24.59},{"session":"S902821","rupees":13.20},{"session":"S901216","rupees":106.60}]. Pooja's ledger says those three are 14439 paise; Tenzin's TotalRefund(data []byte) (int, error) returns 14438. Fix it so that it returns the exact total in whole paise for any list: the Check also tries S900388, S901296 and S901133 at 54.01, 18.22 and 4.10 rupees (7633 paise), a single refund of 0.29 rupees (29 paise), and an empty list (0).
It returns an error when the JSON is broken and when a refund has no rupees number (missing, or the text "24.59").
Joseph will ask: why does BijliGo's own JSON carry money as an int of paise, never as rupees with decimals?
What you will learn
- encoding/json: the standard library's JSON package.
- json.Marshal and json.Unmarshal: a Go value to JSON, and JSON into a Go value.
- Struct tags: json:"charger_id" gives a field its JSON name; omitempty leaves an empty one out.
- Exported fields only: JSON sees only fields that start with a capital letter.
- map[string]any: JSON you have no struct for, where every number is a float64.
- json.Decoder and DisallowUnknownFields: refusing fields you do not know.
- time.Time in JSON: the RFC 3339 text 2026-10-14T18:30:00+05:30.
More in Real programs
Build "JSON" yourself
BijliGo, a Bengaluru EV charging network, drops chargers at the evening peak. You rewrite its backend in Go. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Go are free. This one comes with the all labs plan.