3.6.6 Lab - Parse Different Data Types With Python
3.6.6 Lab - Parse Different Data Types With Python
Objectives
Part 1: Launch the DEVASC VM
Part 2: Parse XML in Python
Part 3: Parse JSON in Python
Part 4: Parse YAML in Python
Background / Scenario
Parsing means analyzing a message, breaking it into its component parts, and understanding the purpose of
each part in context. When messages are transmitted between computers, they travel as a stream of
characters. Those characters are effectively a string. That message needs to be parsed into a semantically-
equivalent data-structure containing data of recognized types (e.g., integers, floats, strings, and Booleans)
before the data can be interpreted and acted upon.
In this lab, you will use Python to parse each data format in turn: XML, JSON, and YAML. We'll walk through
code examples and talk about how each parser works.
Required Resources
• 1 PC with operating system of your choice
• Virtual Box or VMWare
• DEVASC Virtual Machine
Instructions
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 1 of 6 www.netacad.com
Lab - Parse Different Data Types with Python
</target>
<default-operation>merge</default-operation>
<test-option>set</test-option>
<config>
<int8.1
xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"
nc:operation="create"
xmlns="http://netconfcentral.org/ns/test">9</int8.1>
</config>
</edit-config>
</rpc>
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 2 of 6 www.netacad.com
Lab - Parse Different Data Types with Python
"access_token":"ZDI3MGEyYzQtNmFlNS00NDNhLWFlNzAtZGVjNjE0MGU1OGZmZWNmZDEwN2ItY
TU3",
"expires_in":1209600,
"refresh_token":"MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTEyMzQ1
Njc4",
"refreshtokenexpires_in":7776000
}
In Python scripts, the Python json library can be used to parse JSON into Python native data structures, and
serialize data structures back out as JSON. The Python yaml library can be used to convert the data to
YAML.
The following program uses both modules to parse the above JSON data, extract and print data values, and
output a YAML version of the file. It uses the json library loads() method to parse a string into which the file
has been read. It then uses normal Python data references to extract values from the resulting Python data
structure. Finally, it uses the yaml library dump() function to serialize the Python data back out as YAML, to
the terminal.
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 3 of 6 www.netacad.com
Lab - Parse Different Data Types with Python
Step 2: Run the script to print the JSON data and then modify it to print data of interest.
a. Save and run your script. You should see the following output.
devasc@labvm:~/labs/devnet-src/parsing$ python3 parsejson.py
{'access_token': 'ZDI3MGEyYzQtNmFlNS00NDNhLWFlNzAtZGVjNjE0MGU1OGZmZWNmZDEwN2ItYTU3',
'expires_in': 1209600, 'refresh_token':
'MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTEyMzQ1Njc4',
'refreshtokenexpires_in': 7776000}
devasc@labvm:~/labs/devnet-src/parsing$
b. Add print statements that display the token value and how many seconds until the token expires.
print("The access token is: {}".format(ourjson['access_token']))
print("The token expires in {} seconds.".format(ourjson['expires_in']))
c. Save and run your script. You should see the following output.
devasc@labvm:~/labs/devnet-src/parsing$ python3 parsejson.py
{'access_token': 'ZDI3MGEyYzQtNmFlNS00NDNhLWFlNzAtZGVjNjE0MGU1OGZmZWNmZDEwN2ItYTU3',
'expires_in': 1209600, 'refresh_token':
'MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTEyMzQ1Njc4',
'refreshtokenexpires_in': 7776000}
1209600
The access token is ZDI3MGEyYzQtNmFlNS00NDNhLWFlNzAtZGVjNjE0MGU1OGZmZWNmZDEwN2ItYTU3
The token expires in 1209600 seconds
devasc@labvm:~/labs/devnet-src/parsing$
devasc@labvm:~/labs/devnet-src/parsing$
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 4 of 6 www.netacad.com
Lab - Parse Different Data Types with Python
access_token: ZDI3MGEyYzQtNmFlNS00NDNhLWFlNzAtZGVjNjE0MGU1OGZmZWNmZDEwN2ItYTU3
expires_in: 1209600
refresh_token: MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTEyMzQ1Njc4
refreshtokenexpires_in: 7776000
Step 2: Run the script to print the YAML data and then modify it to print data of interest.
a. Save and run your script. You should see the following output.
devasc@labvm:~/labs/devnet-src/parsing$ python3 parseyaml.py
{'access_token': 'ZDI3MGEyYzQtNmFlNS00NDNhLWFlNzAtZGVjNjE0MGU1OGZmZWNmZDEwN2ItYTU3',
'expires_in': 1209600, 'refresh_token':
'MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTEyMzQ1Njc4',
'refreshtokenexpires_in': 7776000}
devasc@labvm:~/labs/devnet-src/parsing$
b. Add print statements that display the token value and how many seconds until the token expires.
print("The access token is {}".format(ouryaml['access_token']))
print("The token expires in {} seconds.".format(ouryaml['expires_in']))
c. Save and run your script. You should see the following output.
devasc@labvm:~/labs/devnet-src/parsing$ python3 parseyaml.py
{'access_token': 'ZDI3MGEyYzQtNmFlNS00NDNhLWFlNzAtZGVjNjE0MGU1OGZmZWNmZDEwN2ItYTU3',
'expires_in': 1209600, 'refresh_token':
'MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTEyMzQ1Njc4',
'refreshtokenexpires_in': 7776000}
The access token is ZDI3MGEyYzQtNmFlNS00NDNhLWFlNzAtZGVjNjE0MGU1OGZmZWNmZDEwN2ItYTU3
The token expires in 1209600 seconds.
devasc@labvm:~/labs/devnet-src/parsing$
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 5 of 6 www.netacad.com
Lab - Parse Different Data Types with Python
b. Save and run your script. You should see the following output. Notice that the output looks just like the
myfile.json.
devasc@labvm:~/labs/devnet-src/parsing$ python3 parseyaml.py
<output from previous steps omitted>
{
"access_token": "ZDI3MGEyYzQtNmFlNS00NDNhLWFlNzAtZGVjNjE0MGU1OGZmZWNmZDEwN2ItYTU3",
"expires_in": 1209600,
"refresh_token": "MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTEyMzQ1Njc4",
"refreshtokenexpires_in": 7776000
}
devasc@labvm:~/labs/devnet-src/parsing$
End of Document
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 6 of 6 www.netacad.com