'Router Uplink', 'enabled': True, 'ipv4':
{'address':
[{'ip': '192.168.0.2', 'netmask':
'255.255.255.0'}]}}}
You can now modify any of the key/value pairs, as in this
example, where the description is changed:
Click here to view code image
>>> json_dict["interface"]["description"] =
"Backup Link"
>>> print(json_dict)
{'interface': {'name': 'GigabitEthernet1',
'description': 'Backup
Link', 'enabled': True, 'ipv4': {'address':
[{'ip': '192.168.0.2',
'netmask': '255.255.255.0'}]}}}
In order to save the new json object back to a file, you
have to use the dump() function (without the s) to
convert the Python dictionary back into a JSON file
object. To make it easier to read, you can use the indent
keyword:
Click here to view code image
with open("json_sample.json", "w") as fh:
json.dump(json_dict, fh, indent = 4)
Now if you load the file again and print, you can see the
stored changes, as shown in Example 5-3.
Example 5-3 Loading the JSON File and Printing the
Output to the Screen
Click here to view code image
>>> with open ("json_sample.json") as data:
json_data = data.read()
print(json_data)
{
"interface": {
"name": "GigabitEthernet1",
"description": "Backup Link",
"enabled": true,
"ipv4": {