Codington
0 of 6 problems solved • 0%

Exercise prompt

Update a Preference

A settings file can contain fields that your program does not use. Decode it, change one top-level field, then encode the complete object.

Example

A reader adds a tag to a decoded list before saving it again.

import json tags = json.loads('["travel", "maps"]') tags.append("notes") saved = json.dumps(tags) print(json.loads(saved)) # ['travel', 'maps', 'notes']

Make the change on the decoded Python value. A settings record uses the same decode–edit–encode sequence with a dictionary.

Your task

Implement update_setting(text, key, value). Return a JSON string with the given top-level field set to the new value. The field can be new. Preserve the remaining fields, including nested objects.

update_setting('{"theme":"dark","scale":2}', "scale", 3) # JSON for {"theme": "dark", "scale": 3}