bhylu.com

Advertisements

Code Challenge

Parse a small JSON booking, increase its seat count, and serialise the updated structure as JSON text.

Understand the idea

Write your own solution before opening the worked answer. Predict the output from the supplied values, then compare the actual result. Check both an ordinary case and the boundary described below; a passing example is more useful when you can explain why it passes.

Where to try it

Open this example with Try in playground. The code loads as editable text and does not run until you select Run. The first run downloads Python. Each run starts fresh; temporary files created by a script are not files on your PC.

Your task

Parse a small JSON booking, increase its seat count, and serialise the updated structure as JSON text.

Show the worked solution
Python

import json
record = json.loads('{"name": "Asha", "seats": 2}')
record["seats"] += 1
print(json.dumps(record, sort_keys=True))

Check the result

The JSON output contains name Asha and seats 3.

Test one more case

Reject a record whose seats value is the text two instead of a number.