Keep a subtotal out of the item rows
Use the opposite Shift key for paired punctuation. · 189 words
Structured schemas can separate row data from totals. The source cart from Harbor Books contains a Notebook, quantity 2, unit_price 4.50, and a Canvas tote, quantity 1, unit_price 12.00. Its subtotal is 21.00. Ask for an order object with items and totals. Each item contains product, quantity, unit_price, and line_total. Put subtotal, tax, and grand_total only in a top-level totals object. The expected line totals are 9.00 and 12.00. Check the result twice: no item has a subtotal key, and the line_total values sum to 21.00. This prevents repeated summary values from drifting apart.
Try a museum order: 3 adult tickets at 14.00 and 1 child ticket at 6.00; subtotal 48.00, tax 3.84, grand_total 51.84. Specify one items array and one totals object, and say that totals appear once. The item line_total values should be 42.00 and 6.00. List every key in the first item. It should contain no subtotal, tax, or grand_total. Then confirm that 42.00 + 6.00 equals the separate subtotal and that 48.00 + 3.84 equals the grand_total. These checks catch both common errors: placing a subtotal inside every row and calculating from the wrong level.
Check your recall
Where should an order subtotal be stored, and what checks prove that it was kept out of the item rows?
Check your answer
Store the subtotal once in the order's totals object, not in any item. Verify that no item contains a subtotal key and that the sum of all item line_total values equals the stored subtotal.
Sources and credits
Original KeyTouch lesson. Background reading · JSON Schema · Understanding JSON Schema.