DevNet Associate -Understanding Data Formats
DevNet Associate -Understanding Data Formats
Show Menu
3.5.6 Test-Driven Development (TDD) / Software Development and Design
/ Understanding Data Formats
Software Development and
3 Design
3.5.7 Lab - Create a Python Unit Test
3.6.1
3.6.2 XML
Data Formats
3.6.3 JSON
3.6.4 YAML Rest APIs, which you'll learn about in the next module, let
you exchange information with remote services and
3.6.5 Parsing and Serializing equipment. So do interfaces built on these APIs, including
purpose-dedicated command-line interface tools and
integration software development kits (SDKs) for popular
Lab - Parse Different Data Types
3.6.6 programming languages.
with Python
https://contenthub.netacad.com/devnet/3.6.1 1/16
7/23/24, 9:00 AM DevNet Associate -Understanding Data Formats
Today, the three most popular standard formats for
exchanging information with remote APIs are XML, JSON,
and YAML. The YAML standard was created as a superset
of JSON, so any legal JSON document can be parsed and
Show Menu converted to equivalent YAML, and (with some limitations
3.5.6 Test-Driven Development (TDD)
and exceptions) vice-versa. XML, an older standard, is
Software Development and not as simple to parse, and in some cases, it is only partly
3 Design
3.5.7 Lab - Create a Python Unit Test (or not at all) convertible to the other formats. Because
XML is older, the tools for working with it are quite
mature.
3.6 Understanding Data Formats
Parsing XML, JSON, or YAML is a frequent requirement of
3.6.1 Data Formats interacting with application programming interfaces
(APIs). Later in this course, you will learn more about
REpresentational State Transfer (REST) APIs. For now, it is
3.6.2 XML sufficient for you to understand that an oft-encountered
pattern in REST API implementations is as follows:
3.6.3 JSON
1. Authenticate, usually by POSTing a user/password
combination and retrieving an expiring token for use in
3.6.4 YAML authenticating subsequent requests.
2. Execute a GET request to a given endpoint
3.6.5 Parsing and Serializing (authenticating as required) to retrieve the state of a
resource, requesting XML, JSON, or YAML as the
Lab - Parse Different Data Types output format.
3.6.6 3. Modify the returned XML, JSON, or YAML.
with Python
4. Execute a POST (or PUT) to the same endpoint (again,
Software Development and authenticating as required) to change the state of the
3.7
Design Summary resource, again requesting XML, JSON, or YAML as
the output format and interpreting it as needed to
determine if the operation was successful.
Understanding and Using
4
APIs
5 Network Fundamentals
3.6.2
XML
Application Deployment and
6
Security
Extensible Markup Language (XML) is a derivative of
Structured, Generalized Markup Language (SGML), and
Infrastructure and
7 also the parent of HyperText Markup Language (HTML).
Automation
XML is a generic methodology for wrapping textual data
in symmetrical tags to indicate semantics. XML filenames
Cisco Platforms and typically end in ".xml".
8
Development
An Example XML Document
https://contenthub.netacad.com/devnet/3.6.1 2/16
7/23/24, 9:00 AM DevNet Associate -Understanding Data Formats
For example, a simple XML document might look like this:
3.6.2 XML
This example simulates information you might receive
from a cloud computing management API that is listing
3.6.3 JSON
virtual machine instances.
3.6.4 YAML
For example, a data field cannot contain text that includes
the < or > symbols, used by XML to demarcate tags. If
3.6.5 Parsing and Serializing writing your own XML (without a schema), it is common to
use HTML entity encodings to encode such characters. In
Lab - Parse Different Data Types this case the characters can be replaced with their
3.6.6
with Python equivalent < and > entity encodings. You can use a
similar strategy to represent a wide range of special
Software Development and symbols, ligature characters, and other entities.
3.7
Design Summary
Note that if you are using XML according to the
requirements of a schema, or defined vocabulary and
Understanding and Using
4 hierarchy of tag names, (this is often the case when
APIs
interacting with APIs such as NETCONF) you are not
permitted to use HTML entities. In the rare case when
special characters are required, you can use the
5 Network Fundamentals characters' numeric representations, which for the less-
than and greater-than signs, are < and > respectively.
XML Prologue
https://contenthub.netacad.com/devnet/3.6.1 4/16
7/23/24, 9:00 AM DevNet Associate -Understanding Data Formats
The XML prologue is the first line in an XML file. It has a
special format, bracketed by <? and ?> . It contains the
tag name xml and attributes stating the version and a
character encoding. Normally the version is "1.0", and the
Show Menu character encoding is "UTF-8" in most cases; otherwise,
3.5.6 Test-Driven Development (TDD)
"UTF-16". Including the prologue and encoding can be
Software Development and important in making your XML documents reliably
3 Design
3.5.7 Lab - Create a Python Unit Test interpretable by parsers, editors, and other software.
Comments in XML
3.6 Understanding Data Formats
XML files can include comments, using the same
3.6.1 Data Formats commenting convention used in HTML documents. For
example:
3.6.2 XML
<!-- This is an XML comment. It can go
anywhere -->
3.6.3 JSON
XML Attributes
3.6.4 YAML
XML lets you embed attributes within tags to convey
additional information. In the following example, the XML
3.6.5 Parsing and Serializing
version number and character encoding are both inside
the xml tag. However, the vmid and type elements
Lab - Parse Different Data Types
3.6.6 could also be included as attributes in the xml tag:
with Python
<rpc message-id="101"
3.6.5 Parsing and Serializing xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"
<kill-session>
Lab - Parse Different Data Types <session-id>4</session-id>
3.6.6
with Python </kill-session>
</rpc>
Software Development and
3.7
Design Summary
Interpreting XML
Understanding and Using
4 In the above example, what is represented is intended as
APIs
a list or one-dimensional array (called 'instances') of
objects (each identified as an 'instance' by bracketing
5 tags). Each instance object contains two key-value pairs
Network Fundamentals
denoting a unique instance ID and VM server type. A
semantically-equivalent Python data structure might be
Application Deployment and declared like this:
6
Security
vms [
Infrastructure and {
7 {"vmid": "0101af9811012"},
Automation
{"type": "t1.nano"}
},
Cisco Platforms and {
8
Development {"vmid": "0102bg8908023"},
{"type": "t1.micro"}
https://contenthub.netacad.com/devnet/3.6.1 6/16
7/23/24, 9:00 AM DevNet Associate -Understanding Data Formats
}
]
JSON
Understanding and Using
4
APIs
JSON, or JavaScript Object Notation, is a data format
derived from the way complex object literals are written in
5 Network Fundamentals JavaScript (which is in turn, similar to how object literals
are written in Python). JSON filenames typically end in
".json".
Application Deployment and
6
Security
Here is a sample JSON file, containing some key/value
pairs. Notice that two values are text strings, one is a
Infrastructure and boolean value, and two are arrays:
7
Automation
{
Cisco Platforms and "edit-config":
8 {
Development
"default-operation": "merge",
"test-operation": "set",
https://contenthub.netacad.com/devnet/3.6.1 7/16
7/23/24, 9:00 AM DevNet Associate -Understanding Data Formats
"some-integers": [2,3,5,7,9],
"a-boolean": true,
"more-numbers": [2.25E+2,-1.0735],
}
Show Menu }
3.5.6 Test-Driven Development (TDD)
Software Development and
3 Design JSON Basic Data Types
3.5.7 Lab - Create a Python Unit Test
JSON basic data types include numbers (written as
positive and negative integers, as floats with decimal, or
3.6 Understanding Data Formats
in scientific notation), strings, Booleans ('true' and 'false'),
or nulls (value left blank).
3.6.1 Data Formats
JSON Objects
3.6.2 XML
As in JavaScript, individual objects in JSON comprise
key/value pairs, which may be surrounded by braces,
3.6.3 JSON individually:
3.6.5 Parsing and Serializing This example depicts an object with a string value (for
example, the word 'value'). A number or Boolean would
Lab - Parse Different Data Types not be quoted.
3.6.6
with Python
JSON Maps and Lists
Software Development and
3.7
Design Summary
Objects may also contain multiple key/value pairs,
separated by commas, creating structures equivalent to
complex JavaScript objects, or Python dictionaries. In this
Understanding and Using
4 case, each individual key/value pair does not need its own
APIs
set of brackets, but the entire object does. In the above
example, the key "edit-config" identifies an object
containing five key/value pairs.
5 Network Fundamentals
No Comments in JSON
Cisco Platforms and
8
Development
Unlike XML and YAML, JSON does not support any kind
of standard method for including unparsed comments in
https://contenthub.netacad.com/devnet/3.6.1 8/16
7/23/24, 9:00 AM DevNet Associate -Understanding Data Formats
code.
Whitespace Insignificant
3.6.4
3.6.2 XML
YAML
3.6.3 JSON
- 7
- 9
test-operation: set
...
Show Menu
3.5.6 Test-Driven Development (TDD)
YAML File Structure
Software Development and
3 Design
3.5.7 Lab - Create a Python Unit Test As shown in the example, YAML files conventionally open
with three dashes ( --- alone on a line) and end with
three dots ( ... also alone a line). YAML also
3.6 Understanding Data Formats
accommodates the notion of multiple "documents" within
a single physical file, in this case, separating each
3.6.1 Data Formats document with three dashes on its own line.
Understanding and Using YAML also offers convenient ways of encoding multi-line
4 string literals (more below).
APIs
Basic Objects
5 Network Fundamentals
In YAML, basic (and complex) data types are equated to
keys. Keys are normally unquoted, though they may be
Application Deployment and quoted if they contain colons (:) or certain other special
6 characters. Keys also do not need to begin with a letter,
Security
though both these features conflict with the requirements
of most programming languages, so it is best to stay
Infrastructure and away from them if possible.
7
Automation
A colon (:) separates the key and value:
3.6.5 Parsing and Serializing YAML easily represents more complex data types, such
as maps containing multiple key/value pairs (equivalent to
Lab - Parse Different Data Types dictionaries in Python) and ordered lists.
3.6.6
with Python
Maps are generally expressed over multiple lines,
Software Development and beginning with a label key and a colon, followed by
3.7
Design Summary
members, indented on subsequent lines:
5 Network Fundamentals
Lists (arrays) are represented in a similar way, but with
optionally-indented members preceded by a single dash
Application Deployment and and space:
6
Security
mylist:
- 1
Infrastructure and
7 - 2
Automation
- 3
https://contenthub.netacad.com/devnet/3.6.1 11/16
7/23/24, 9:00 AM DevNet Associate -Understanding Data Formats
Show Menu
3.5.6 Test-Driven Development (TDD) Long Strings
Software Development and
3 Design
3.5.7 Lab - Create a Python Unit Test You can represent long strings in YAML using a 'folding'
syntax, where linebreaks are presumed to be replaced by
spaces when the file is parsed/consumed, or in a non-
3.6 Understanding Data Formats folding syntax. Long strings cannot contain escaped
special characters, but may (in theory) contain colons,
3.6.1 Data Formats though some software does not observe this rule.
# this is a comment
5 Network Fundamentals
More YAML Features
Application Deployment and YAML has many more features, most often encountered
6
Security when using it in the context of specific languages, like
Python, or when converting to JSON or other formats. For
example, YAML 1.2 supports schemas and tags, which
Infrastructure and
7 can be used to disambiguate interpretation of values. For
Automation
example, to force a number to be interpreted as a string,
you could use the !!str string, which is part of the YAML
Cisco Platforms and "Failsafe" schema:
8
Development
mynumericstring: !!str 0.1415
https://contenthub.netacad.com/devnet/3.6.1 12/16
7/23/24, 9:00 AM DevNet Associate -Understanding Data Formats
3.6.5
Show Menu
3.5.6 Test-Driven Development (TDD)
Parsing and Serializing
Software Development and
3 Design
3.5.7 Lab - Create a Python Unit Test
Parsing means analyzing a message, breaking it into its
component parts, and understanding their purposes in
3.6 Understanding Data Formats
context. When messages are transmitted between
computers, they travel as a stream of characters, which is
3.6.1 Data Formats effectively a string. This needs to be parsed into a
semantically-equivalent data-structure containing data of
3.6.2 XML recognized types (such as integers, floats, strings, etc.)
before the application can interpret and act upon the
data.
3.6.3 JSON
Serializing is roughly the opposite of parsing. To
3.6.4 YAML communicate information with a REST interface, for
example, you may be called upon to take locally-stored
data (e.g., data stored in Python dictionaries) and output
3.6.5 Parsing and Serializing this as equivalent JSON, YAML, or XML in string form for
presentation to the remote resource.
Lab - Parse Different Data Types
3.6.6
with Python An Example
Software Development and For example, imagine you wanted to send an initial query
3.7
Design Summary
to some remote REST endpoint, inquiring about the status
of running services. To do this, typically, you would need
to authenticate to the REST API, providing your username
Understanding and Using
4 (email), plus a permission key obtained via an earlier
APIs
transaction. You might have stored username and key in a
Python dictionary, like this:
5 Network Fundamentals
auth = {
"user": {
Application Deployment and "username": "[email protected]",
6
Security "key": "90823ff08409408aebcf4320384"
}
}
Infrastructure and
7
Automation
But the REST API requires these values to be presented
as XML in string form, appended to your query as the
Cisco Platforms and
8 value of a key/value pair called "auth":
Development
https://contenthub.netacad.com/devnet/3.6.1 13/16
7/23/24, 9:00 AM DevNet Associate -Understanding Data Formats
https://myservice.com/status/services?auth=
<XML string containing username and key>
Show Menu The XML itself might need to take this format, with Python
3.5.6 Test-Driven Development (TDD) key values converted to same-name tag pairs, enclosing
Software Development and data values:
3 Design
3.5.7 Lab - Create a Python Unit Test
<user>
<username>[email protected]</username>
3.6 Understanding Data Formats
<key>90823ff08409408aebcf4320384</key>
</user>
3.6.1 Data Formats
5 Network Fundamentals
</service>
</services>
3.6.2 XML
In this case, the untangle library would parse the XML into
a dictionary whose root element (services) contains a list
3.6.3 JSON (service[]) of pairs of key/value object elements denoting
the name and status of each service. You could then
access the 'cdata' value of elements to obtain the text
3.6.4 YAML
content of each XML leaf node. The above code would
print:
3.6.5 Parsing and Serializing
Service B Idle
Lab - Parse Different Data Types
3.6.6
with Python
Popular programming languages such as Python generally
Software Development and incorporate easy-to-use parsing functions that can
3.7
Design Summary accept data returned by an I/O function and produce a
semantically-equivalent internal data structure containing
valid typed data. On the outbound side, they contain
Understanding and Using serializers that do the opposite, turning internal data
4
APIs structures into semantically-equivalent messages
formatted as character strings.
5 Network Fundamentals
7
Infrastructure and
Types with Python
Automation
Cisco Platforms and *In this lab, you will use Python to parse each data format
8 in turn: XML, JSON, and YAML. You will walk through
Development
code examples and investigate how each parser works.
https://contenthub.netacad.com/devnet/3.6.1 15/16
7/23/24, 9:00 AM DevNet Associate -Understanding Data Formats
You will complete the following objectives:
3.6.1 Data Formats 3.5 3.7
Code Review and T… Software Developm…
3.6.2 XML
3.6.3 JSON
3.6.4 YAML
5 Network Fundamentals
Infrastructure and
7
Automation
https://contenthub.netacad.com/devnet/3.6.1 16/16