How do you evolve a JSON response without breaking live clients?
Question
GET /v1/orders/{id} currently returns {"id": "ord_7", "price": 19.99} to web users plus iOS v2.1 and Android v3.0 apps already in the wild. Product wants to replace float price with "total": {"amount_cents": 1999, "currency": "USD"}.
- Why does replacing
pricein place break older mobile builds even if your web app ships at the same time? - Which JSON response changes are backwards-compatible versus breaking?
- Design a safe rollout for
totalinside/v1/orders/{id}, and explain when to send RFC 9745Deprecationand RFC 8594Sunsetheaders on a retiring/v1/resource.
Show hints (3)
- "CLIENTS UPGRADE SLOWLY"
You control your web deploy, not phones. Mobile versions stay installed for months and crash if
pricedisappears or changes type. - "EXPAND THEN CONTRACT"
Add
totalalongsidepricefirst so both old and new clients work. Removepriceonly after telemetry shows zero old callers. - "RETIRING V1 RESOURCE"
Mark
pricedeprecated in OpenAPI while evolving/v1/. Send RFC 9745Deprecationand RFC 8594Sunsetheaders on/v1/orders/{id}only when retiring that URI for/v2/.
Show checked explanation
Never rename, remove, or retype a live JSON field in one deploy because older mobile apps stay active for months. Evolve /v1/ with expand-and-contract: add total while keeping price, mark price deprecated in OpenAPI, and drop it only once telemetry shows zero legacy callers. Use RFC 9745 Deprecation and RFC 8594 Sunset headers when retiring the /v1/ URI itself for /v2/.
from email.utils import formatdate def serialize_order(oid, cents, currency, legacy=True): body = {"id": oid, "total": {"amount_cents": cents, "currency": currency}} if legacy: body["price"] = round(cents / 100, 2) return body def sunset_headers(dep_ts, sunset_ts, doc_url): return {"Deprecation": f"@{dep_ts}", "Sunset": formatdate(sunset_ts, usegmt=True), "Link": f'<{doc_url}>; rel="deprecation"'} print("expand:", serialize_order("ord_7", 1999, "USD", legacy=True)) print("contract:", serialize_order("ord_7", 1999, "USD", legacy=False)) print("v1_retire:", sunset_headers(1798761600, 1814400000, "/docs/orders-v2"))
expand: {'id': 'ord_7', 'total': {'amount_cents': 1999, 'currency': 'USD'}, 'price': 19.99}
contract: {'id': 'ord_7', 'total': {'amount_cents': 1999, 'currency': 'USD'}}
v1_retire: {'Deprecation': '@1798761600', 'Sunset': 'Thu, 01 Jul 2027 00:00:00 GMT', 'Link': '</docs/orders-v2>; rel="deprecation"'}Ran the dual-shape serializer and RFC 9745/8594 header helper on CPython 3.12.10 against simulated old/new clients and a local http.server. Checked by an editor other than the author.
Walkthrough
A backend deploy updates all servers in minutes, while mobile users keep old app binaries for months. Changing a response field in place breaks every client still expecting price as a JSON number.
Judge every JSON change by whether an unmodified live client still parses and acts on it:
| Response change | Breaking? | Safe alternative |
|---|---|---|
Add optional total object | No | Ship directly in v1 |
Rename price to total | Yes | Serve both during overlap |
Change 19.99 to "19.99" | Yes | Add new field with new type |
Allow price to be null | Yes | Keep non-null default or add new field |
| Add new closed enum value | Often yes | Version enum or negotiate client capability |
Roll out field changes inside /v1/orders/{id} in four stages:
- Expand: Ship
serialize_order(..., legacy=True)so responses carry bothprice(19.99) andtotal({"amount_cents": 1999, "currency": "USD"}). Old iOSv2.1and Androidv3.0readpriceand ignoretotal. Migrated iOSv2.2and Androidv3.1readtotal. - Deprecate the field and migrate: Mark
pricewithdeprecated: truein OpenAPI 3.1 and update web and mobile apps to readtotal. Do not sendSunsetwhile/v1/orders/{id}remains operational. - Measure drain: Log
X-Client-VersionorUser-Agentto track how many callers still runv2.1orv3.0. Prompt remaining mobile users to upgrade. - Contract or retire
/v1/: Dropprice(legacy=False) only once legacy callers reach zero. If partners pin/v1/longer, keep/v1/orders/{id}intact and launch/v2/orders/{id}withtotalonly. When retiring the/v1/URI itself, emit RFC 9745Deprecation: @1798761600(2027-01-01), RFC 8594Sunset: Thu, 01 Jul 2027 00:00:00 GMT, andLink: </docs/orders-v2>; rel="deprecation". Both headers apply to the URI, and RFC 8594 reservesSunsetfor resources that will stop responding.
Common mistakes
- Renaming
pricetototalin one release because the web frontend updated in the same PR. Older mobile and partner clients break immediately. - Making a previously non-null response field nullable. Typed Swift, Kotlin, and TypeScript clients throw decode errors on
null. - Dropping a deprecated field on a calendar date without checking client-version metrics first, silently breaking active partner integrations.
- Changing a field's units (dollars
19.99to cents1999) under the same key nameprice. Clients keep parsing cleanly while showing 100x wrong prices.
Follow-up questions
- "When should you bump to /v2/ instead of evolving /v1/ with expand-and-contract?" Stay on
/v1/for additive fields and small renames you can overlap cleanly. Introduce/v2/when core resource shapes, pagination rules, auth models, or error semantics change so much that carrying both inside/v1/would confuse callers. - "Why can adding a new value to a response enum break older clients?" Many compiled clients map JSON strings to a closed enum with no fallback case. Returning a brand-new status like
"partially_refunded"crashes decoding unless older apps already have anunknownbranch. - "What makes a request schema change backwards-compatible versus breaking?" Request compatibility is the mirror image of responses. Making an existing request field optional or widening accepted enum values is safe. Adding a new required field or tightening a length limit breaks existing callers.
What a strong answer covers
Classifies additive vs breaking JSON changes and explains why mobile clients force overlap windows. Separates in-place field expand-and-contract (OpenAPI deprecated: true + telemetry) from resource-level /v1/ retirement using RFC 9745 Deprecation and RFC 8594 Sunset.
Open in interactive practice deck →