Working with JSON
Completed
Read one line of JSON describing an order: {"items": [{"name": ..., "price": ..., "qty": ...}]}. Print each item as name xqty, then output a JSON object {"count": N, "total": T} with sorted keys.
Input
One line of JSON.
Output
The items, then the JSON summary.
Example
Input
{"items": [{"name": "pen", "price": 1.5, "qty": 4}, {"name": "pad", "price": 3, "qty": 1}]}Output
pen x4
pad x1
{"count": 2, "total": 9.0}
Editor
Python
Hints
json.loads turns JSON text into Python dicts and lists.
json.dumps turns Python data back into JSON text.
Solution explanation
JSON maps naturally to Python: objects ↔ dicts, arrays ↔ lists, strings, numbers, true/false/null.
Show solution
import json
order = json.loads(input())
total = 0
for item in order["items"]:
print(f"{item['name']} x{item['qty']}")
total += item["price"] * item["qty"]
print(json.dumps({"total": round(total, 2), "count": len(order["items"])}, sort_keys=True))