Learn Practice Daily golf Reference
Real-world refactors·Lesson 4 of 6·11 min·0/6 exercisesPro

Reshape a JSON payload

Launch week, Thursday: the mobile payload reshaped for the new API. One slip that asks at each match for camelCase keys (a value must stay), dd and a stray comma for the structure, % to check the shape, ci" and cgn for values.

Scene

Thursday of launch week, 9:00 a.m. The mobile team's order payload is shaped for the old app: snake_case keys, a legacy block nobody reads, "TODO" where a guest's name should be, and "sku" where the new API says "id".

JSON is all wrappings: quotes, brackets, braces. Every tool you have for wrappings applies.

From memory

Warm-up from earlier chapters

Two from earlier chapters, from memory, before the new idea. Two wrong tries show the answer.

Move every line that mentions order into jar a.
press the keys…13 keys
1 of 2
Move every line that mentions order into jar a.

Pattern, structure, value

Three kinds of change, three tools. A change with a pattern in it (order_id → orderId) is one slip: :%s/_\(\a\)/\u\1/g circles the letter after each underscore and puts it back as a capital. A change to the structure (a whole key gone) is lines and commas: dd, then the comma the line above no longer needs. A change to values is the burrito: ci" from anywhere in the string.

A slip matches text: it can't tell a key from a value. When a value has the same shape as what you're changing, add the c flag and answer each match yourself: y to change it, n to leave it.

Before
"delivery_zone_id": 3,
:s/_\(\a\)/\u\1/g↵
"deliveryZoneId": 3,
Each underscore and the letter after it become that letter as a capital: deliveryZoneId.
Boss round

Stage 1 · camelCase

9:10. Every snake_case key becomes camelCase, in one slip. One value has underscores too: "ready_for_pickup" is what the kitchen system reads, letter for letter, so it stays.

Stuck? Show a hint:%s/_\(\a\)/\u\1/gc<CR> then y y y n n y y
Fix the marked spots · 4 left
1{
1 "order": {
2 "order_id": 4401,
3 "customer_name": "TODO",
4 "kitchen_status": "ready_for_pickup",
5 "delivery_zone_id": 3,
6 "items": [
7 { "sku": "al-pastor", "qty": 2 },
8 { "sku": "birria", "qty": 1 },
9 { "sku": "horchata", "qty": 2 }
10 ],
11 "legacy": { "fax": null, "pager": null }
12 }
13}
NORMALLoading the editor…0.0s1:1

This exercise needs a keyboard. Read on, and play it at your computer.

Boss round

Stage 2 · No legacy

9:20. The "legacy" line goes, and the ], above it loses its comma (JSON allows no comma after the last key). The cursor is on the legacy line.

Stuck? Show a hint
Pro
The rest of this lesson is in Pro

The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.

See Pro

This exercise needs a keyboard. Read on, and play it at your computer.

Check yourself

Check the shape

1{
2 "order": {
1 "orderId": 4401,
2 "customerName": "TODO",
3 "kitchenStatus": "ready_for_pickup",
4 "deliveryZoneId": 3,
5 "items": [
6 { "sku": "al-pastor", "qty": 2 },
7 { "sku": "birria", "qty": 1 },
8 { "sku": "horchata", "qty": 2 }
9 ]
10 }
11}
NORMAL
Keys pressed: %

A deleted line is where JSON breaks. With the cursor on the brace after "order":, you press %. Where does it land?

Pick an answer (or press its number), then watch the engine prove it.
Boss round

Stage 3 · The values

9:35. "TODO" becomes "Guest" (the cursor is on its line), and each of the three "sku" keys becomes "id".

Stuck? Show a hint
Pro
The rest of this lesson is in Pro

The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.

See Pro

This exercise needs a keyboard. Read on, and play it at your computer.

From memory

Reshaping, from memory

Press the keys.

Change the string's text to Guest, from anywhere inside it.
scroll here, then press the keys9 keys
1 of 3
Change the string's text to Guest, from anywhere inside it.