Describe parsing of common data format (XML, JSON, and YAML) to Python data structures

📘Cisco DevNet Associate (200-901 DEVASC)


1. What is Parsing?

Parsing means:

Reading structured data (like XML, JSON, or YAML) and converting it into a format that a programming language (like Python) can understand and work with.

In DevNet, this usually means:

  • Receiving data from an API
  • Reading configuration files
  • Processing device responses
  • Automating infrastructure tasks

Most network devices, controllers, and cloud platforms return data in JSON, XML, or YAML format.
Your job as a DevNet Associate is to understand how to:

  1. Read that data
  2. Convert it into Python objects
  3. Access and manipulate it

2. Why Parsing Is Important for DEVASC

In real IT environments:

  • REST APIs return JSON
  • Legacy systems may return XML
  • Automation tools like Ansible use YAML
  • Configuration files may be in YAML or JSON
  • Network controllers (like Cisco platforms) return structured data

If you cannot parse the data, you cannot automate anything.

For the exam, you must understand:

  • How each format maps to Python data types
  • How to load/parse each format in Python
  • How to access values inside the parsed structure
  • Differences between the formats

3. JSON Parsing in Python

What is JSON?

JSON = JavaScript Object Notation

It is:

  • Lightweight
  • Easy to read
  • Most common format used in REST APIs
  • Default data format for modern APIs

Example JSON (API response):

{
  "device": "Router1",
  "status": "active",
  "interfaces": ["Gig0/0", "Gig0/1"]
}

JSON → Python Data Structure Mapping

JSON TypePython Type
Object {}Dictionary (dict)
Array []List
Stringstr
Numberint / float
true/falseTrue / False
nullNone

This mapping is very important for the exam.


Parsing JSON in Python

Python has a built-in module called:

import json

Parse JSON string:

import json

json_data = '{"device": "Router1", "status": "active"}'

data = json.loads(json_data)

Now data is a Python dictionary.


Accessing Data

print(data["device"])

Output:

Router1

If JSON contains a list:

print(data["interfaces"][0])

Parsing JSON from API Response

In automation, JSON usually comes from an API.

Example:

import requests

response = requests.get("https://api.example.com/devices")
data = response.json()

response.json() automatically parses JSON into Python dictionary.

For DEVASC, understand:

  • APIs mostly return JSON
  • JSON becomes Python dict + list
  • You access values using dictionary keys

4. XML Parsing in Python

What is XML?

XML = eXtensible Markup Language

It:

  • Uses tags
  • Looks similar to HTML
  • Used in older APIs
  • Used in network device configurations
  • Used in SOAP APIs

Example XML:

<device>
    <name>Router1</name>
    <status>active</status>
</device>

XML Structure

XML contains:

  • Elements (tags)
  • Attributes
  • Text values
  • Nested elements

Unlike JSON, XML does NOT automatically map to dictionary format.


Parsing XML in Python

Python module:

import xml.etree.ElementTree as ET

Parse XML string:

import xml.etree.ElementTree as ET

xml_data = """
<device>
    <name>Router1</name>
    <status>active</status>
</device>
"""

root = ET.fromstring(xml_data)

root is now an Element object.


Accessing Values

print(root.find("name").text)

Output:

Router1

Parsing XML File

tree = ET.parse("devices.xml")
root = tree.getroot()

Important Exam Points About XML

  • XML parsing is more complex than JSON
  • XML elements must be accessed using .find()
  • XML supports attributes:

Example:

<device id="101">

Access attribute:

root.attrib["id"]

XML vs JSON (Exam Tip)

  • JSON → automatically becomes dictionary
  • XML → becomes ElementTree object
  • XML needs more manual navigation
  • JSON is more common in REST APIs

5. YAML Parsing in Python

What is YAML?

YAML = YAML Ain’t Markup Language

It:

  • Is human-readable
  • Used in automation tools
  • Used in configuration files
  • Used in DevOps tools
  • Used in infrastructure-as-code

Example YAML:

device: Router1
status: active
interfaces:
  - Gig0/0
  - Gig0/1

YAML → Python Mapping

YAML maps almost exactly like JSON:

YAMLPython
key: valuedict
– itemlist
true/falseTrue/False
nullNone

YAML is basically more readable JSON.


Parsing YAML in Python

YAML is NOT built-in. You must install:

pip install pyyaml

Then:

import yaml

Parse YAML string:

import yaml

yaml_data = """
device: Router1
status: active
"""

data = yaml.safe_load(yaml_data)

Now data is a Python dictionary.


Access Values

print(data["device"])

Important Exam Points About YAML

  • YAML is indentation-based
  • YAML converts directly into dictionary/list
  • Use yaml.safe_load() (important for security)
  • Common in automation and configuration management

6. Comparing Parsing of XML, JSON, and YAML

FeatureJSONXMLYAML
Most common in REST APIsYesLess commonRare
Converts directly to dictYesNoYes
Built-in Python supportYesYesNo (needs PyYAML)
Easy to parseVery EasyMore ComplexEasy
Used in automation toolsYesSometimesVery Common

7. Understanding Python Data Structures After Parsing

After parsing, you usually get:

  • Dictionary (dict)
  • List
  • Nested dictionaries
  • Nested lists

Example API response:

{
  "devices": [
    {"name": "Router1", "status": "active"},
    {"name": "Switch1", "status": "inactive"}
  ]
}

After parsing:

data["devices"][0]["name"]

To pass the exam, you must understand:

  • How to navigate nested dictionaries
  • How to access list elements
  • How to loop through lists

Example:

for device in data["devices"]:
    print(device["name"])

8. Error Handling During Parsing

In real automation:

  • JSON may be invalid
  • XML may be malformed
  • YAML indentation may be wrong

Example JSON error handling:

try:
    data = json.loads(json_data)
except json.JSONDecodeError:
    print("Invalid JSON")

Exam tip: Understand that parsing can fail.


9. Real IT Use Cases (Exam-Oriented)

You will see parsing used in:

  • REST API responses from controllers
  • Network device status queries
  • Cloud infrastructure APIs
  • Automation scripts
  • Configuration file reading
  • Logging systems

Typical workflow in DevNet:

  1. Send API request
  2. Receive JSON response
  3. Parse into dictionary
  4. Extract specific values
  5. Make automation decision

10. Key Differences You Must Remember for DEVASC

✔ JSON is most important
✔ JSON maps directly to dict/list
✔ XML uses ElementTree
✔ YAML requires PyYAML
✔ Parsing converts text into Python objects
✔ APIs commonly return JSON
✔ Access data using dictionary keys and list indexes


11. Exam-Focused Summary

For the 200-901 DEVASC exam, you must understand:

  • What parsing means
  • How JSON, XML, and YAML differ
  • How each converts into Python structures
  • Which Python modules are used:
    • json
    • xml.etree.ElementTree
    • yaml
  • How to access nested data
  • Why JSON is most common in APIs
  • Why YAML is common in automation tools
  • Why XML is still used in legacy systems

Final Concept in One Sentence

Parsing is the process of converting structured data formats like XML, JSON, and YAML into Python dictionaries, lists, and objects so that automation scripts can read, modify, and use the data.

Buy Me a Coffee