Task 1: Work With JSON Objects in Python

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Firefox https://cll-ng.cisco.

com/content/xtrac/1

Task 1: Work with JSON Objects in Python


Step 1:
From the home directory of the Student Workstation access the Linux terminal by double-clicking the terminal
icon.
Make the following selection:

Step 2:
Enter the Python Shell using the python command.
Enter the following information:
cisco@cisco:~$ python
Python 2.7.11+ (default, Apr 17 2016, 14:00:29)
[GCC 5.3.1 20160413] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
Step 3:
Create the following dictionary. You will see how a dictionary object with key-value pairs natively maps to
JSON objects of name-value pairs. They are, for all intents and purposes the same thing.
>>> neighbor = {'hostname': 'nxos2', 'os': 'nx-os', 'model': '9396'}
>>>
Step 4:
Verify the data type of variable that is called neighbor using the type function. It is always important to see the
differences of objects that are dictionaries and JSON strings that look like dictionaries. You will see the
difference in the next few steps.
Enter the following information:
>>> type(neighbor)
<type 'dict'>

1 din 3 27.05.2020, 09:54


Firefox https://cll-ng.cisco.com/content/xtrac/1

>>>
Step 5:
Import the JSON module and dump the dictionary that is called neighbors as a JSON string.
Enter the following information:
>>> import json
>>>
>>> print json.dumps(neighbor, indent=4)
{
"model": "9396",
"hostname": "nxos2",
"os": "nx-os"
}
>>>
Note
The function that is called dumps means dump as a string. It is also a helpful way to pretty print dictionaries.
Step 6:
Save the object being dumped as a new variable called data. Print data and then check its data type using the
type function.
Enter the following information:
>>> data = json.dumps(neighbor, indent=4)
>>>

First print it as a string literal.

>>> data
'{\n "model": "9396", \n "hostname": "nxos2", \n "os": "nx-os"\n}'
>>>

Now use the print statement.

>>> print data


{
"model": "9396",
"hostname": "nxos2",
"os": "nx-os"
}
>>>
Step 7:
Use the json.loads() functions to load a JSON string and make it a dictionary.
Enter the following information:
>>> data_dict = json.loads(data)
>>> data_dict['os']
u’nx-os
>>>
If you tried access os from data, you would see this error:
>>> data['os']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: string indices must be integers, not str
>>>

2 din 3 27.05.2020, 09:54


Firefox https://cll-ng.cisco.com/content/xtrac/1

Note
APIs are going to return data as JSON strings. This means you will need to use json.loads to work with API
responses. The reverse is true as well. To make an API request, you need to send a JSON string meaning
you’ll need to do a json.dumps(<your-dictionary>) to send your object over the wire.
Step 8:
Close the terminal window by selecting the "x" at the upper left portion of the window. Then, select the Close
Terminal button on the pop-up window.
Make the following selections:

Step 9:
Press the Enter key to end this lab.

3 din 3 27.05.2020, 09:54

You might also like