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.
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.
Warm-up from earlier chapters
Two from earlier chapters, from memory, before the new idea. Two wrong tries show the answer.
order into jar a.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.
"delivery_zone_id": 3, "deliveryZoneId": 3,deliveryZoneId.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 yThis exercise needs a keyboard. Read on, and play it at your computer.
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
The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.
See ProThis exercise needs a keyboard. Read on, and play it at your computer.
Check the shape
A deleted line is where JSON breaks. With the cursor on the brace after "order":, you press %. Where does it land?
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
The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.
See ProThis exercise needs a keyboard. Read on, and play it at your computer.
Reshaping, from memory
Press the keys.
Guest, from anywhere inside it.Guest, from anywhere inside it.