Deviopath › Python

Total of a JSON Order

Medium · Data Processing

json.loads(text) turns a JSON string into Python values: objects become dicts, arrays become lists. Money is often stored in whole cents to avoid floating point rounding errors.

Write order_total(json_text) for an order like {"items": [{"sku": "mug", "price_cents": 1299, "qty": 2}], "discount_cents": 500}. Return the total in cents: the sum of price_cents * qty for every item, minus discount_cents. The discount key may be missing (treat it as 0). The total can never go below 0.

Next: Extract Hashtags