Until now, you wrote the data of your scripts inside the script itself.
JSON is a format to keep this data in a file that any tool can read:
Figure 1 - The Same Data in a Script and in a JSON File
A JSON file looks like a Python dictionary, with keys and values between braces.
In the lab, the script
show.pyreads the fileswitch.json.
Change the address of the switch to 10.0.0.9 in the file and run the script to get the flag.Answer the question below
Enter the flag you obtained.
JSON has its own words for two things you know from Python.
A dictionary is an object and a list is an array:
Figure 2 - An Object and an Array
An object can hold arrays and an array can hold objects, like nested data in Python.
In the lab, add VLAN 30 to the array
vlansofswitch.json.
Runshow.pyto get the flag.Answer the question below
Enter the flag you obtained.
The rules of JSON are stricter than the rules of Python.
Apart from objects and arrays, you write a value in one of these four ways:
Figure 3 - The Four Kinds of JSON Values
A comma after the last value is also an error in JSON.
In the lab, the file
switch.jsonhas three mistakes, soshow.pyfails.
Fix the file to get the flag.Answer the question below
Enter the flag you obtained.
When Python parses a JSON file, it reads the text and turns it into a dictionary or a list.
The functionjson.load()does this:![The function json.load() reads the file switch.json and gives a Python dictionary, so switch["name"] gives "SW1".](https://dphfzu3i54bzu.cloudfront.net/media/10515/python-json-load.png)
Figure 4 - From a JSON File to a Python Dictionary
In a script, you open the file with
open(), then you give it tojson.load():import json with open("switch.json") as file: switch = json.load(file) print(switch["name"]) # Output: SW1withcloses the file when the lines under it are done.In the lab, the file
inventory.jsonholds three switches.
Load it inaudit.pyand print the name of each switch with 48 ports to get the flag.Answer the question below
Enter the flag you obtained.