0% found this document useful (0 votes)
6 views

DevNet Associate -Understanding Data Formats

The document provides an overview of data formats used in software development, particularly focusing on XML, JSON, and YAML. It discusses the importance of these formats for interacting with APIs, including parsing, serializing, and composing messages. Additionally, it covers best practices for structuring XML documents, handling special characters, and utilizing namespaces.

Uploaded by

crio1q
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

DevNet Associate -Understanding Data Formats

The document provides an overview of data formats used in software development, particularly focusing on XML, JSON, and YAML. It discusses the importance of these formats for interacting with APIs, including parsing, serializing, and composing messages. Additionally, it covers best practices for structuring XML documents, handling special characters, and utilizing namespaces.

Uploaded by

crio1q
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

7/23/24, 9:00 AM DevNet Associate -Understanding Data Formats

  DevNet Associate v1.0

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 Understanding Data Formats 


Understanding Data
Formats
3.6.1 Data Formats

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

When controlling these APIs through software, it is helpful


Software Development and
3.7
Design Summary  to be able to receive and transmit information in forms
that are standards-compliant, and machine- and human-
readable. This lets you:
Understanding and Using
4 
APIs Easily use off-the-shelf software components and/or
built-in language tools to convert messages into forms
that are easy for you to manipulate and extract data
5 Network Fundamentals  from, such as data structures native to the
programming language(s) you are using. You can also
convert them into other standard formats that you may
Application Deployment and need for various purposes.
6 
Security Easily write code to compose messages that remote
entities can consume.
Read and interpret received messages yourself to
Infrastructure and
7  confirm that your software is handling them correctly,
Automation
and compose test messages by hand to send to
remote entities.
Cisco Platforms and More easily detect "malformed" messages caused by
8  transmission or other errors interfering with
Development
communication.

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:

<?xml version="1.0" encoding="UTF-8"?>


<!-- Instance list -->
Show Menu <vms>
3.5.6 Test-Driven Development (TDD)
<vm>
Software Development and <vmid>0101af9811012</vmid>
3 Design 
3.5.7 Lab - Create a Python Unit Test <type>t1.nano</type>
</vm>
<vm>
3.6 Understanding Data Formats  <vmid>0102bg8908023</vmid>
<type>t1.micro</type>
3.6.1 Data Formats </vm>
</vms>

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 XML Document Body

For the moment, ignore the first line of the document,


3.6.5 Parsing and Serializing
which is a special part known as the prologue (more on
this below), and the second line, which contains a
Lab - Parse Different Data Types comment. The remainder of the document is called the
3.6.6
with Python body.

Software Development and


3.7  Notice how individual data elements within the body
Design Summary
(readable character strings) are surrounded by
symmetrical pairs of tags, the opening tag surrounded by
Understanding and Using < and > symbols, and the closing tag, which is similar,
4  but with a "/" (slash) preceding the closing tag name.
APIs

Notice also that some tag pairs surround multiple


5 Network Fundamentals  instances of tagged data (for example, the <vm> and
corresponding <vm> tags). The main body of the
document, as a whole, is always surrounded by an
Application Deployment and outermost tag pair (for example, the <vms>...</vms>
6 
Security tag pair) or root tag pair.

The structure of the document body is like a tree, with


Infrastructure and
7  branches coming off the root, containing possible further
Automation
branches, and finally leaf nodes, containing actual data.
Moving back up the tree, each tag pair in an XML
Cisco Platforms and document has a parent tag pair, and so on, until you reach
8  the root tag pair.
Development

User-Defined Tag Names


https://contenthub.netacad.com/devnet/3.6.1 3/16
7/23/24, 9:00 AM DevNet Associate -Understanding Data Formats
XML tag names are user-defined. If you are composing
XML for your own application, best-practice is to pick tag
names that clearly express the meaning of data elements,
their relationships, and hierarchy. Tag names can be
Show Menu repeated as required to enclose multiple data elements
3.5.6 Test-Driven Development (TDD)
(or tagged groupings of elements) of the same type. For
Software Development and example, you could create more <vm> tag pairs to
3 Design 
3.5.7 Lab - Create a Python Unit Test enclose additional <vmid> and <type> groupings.

When consuming XML from an API, tag names and their


3.6 Understanding Data Formats  meanings are generally documented by the API provider,
and may be representative of usage defined in a public
3.6.1 Data Formats namespace schema.

Special Character Encoding


3.6.2 XML

Data is conveyed in XML as readable text. As in most


3.6.3 JSON programming languages, encoding special characters in
XML data fields presents certain challenges.

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.

Application Deployment and


6  To avoid having to individually find and convert special
Security
characters, it is possible to incorporate entire raw
character strings in XML files by surrounding them with
Infrastructure and so-called CDATA blocks. Here is an example:
7 
Automation
<hungarian_sentence><![CDATA[Minden
személynek joga van a neveléshez.]]>
Cisco Platforms and
8  </hungarian_sentence>
Development

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

Software Development and <?xml version="1.0" encoding="UTF-8"?>


3.7 
Design Summary <!-- Instance list -->
<vms>
<vm vmid="0101af9811012" type="t1.nano" />
Understanding and Using
4  <vm vmid="0102bg8908023" type="t1.micro"/>
APIs
</vms>

5 Network Fundamentals  There are a few things to notice here:

Attribute values must always be included in single or


Application Deployment and double quotes.
6  An element may have multiple attributes, but only one
Security
attribute for each specific name.
If an element has no content, you can use a shorthand
Infrastructure and
7  notation in which you put the slash inside the open tag,
Automation rather than including a closing tag.

Cisco Platforms and XML Namespaces


8 
Development
Some XML messages and documents must incorporate a
reference to specific namespaces to specify particular
https://contenthub.netacad.com/devnet/3.6.1 5/16
7/23/24, 9:00 AM DevNet Associate -Understanding Data Formats
tagnames and how they should be used in various
contexts. Namespaces are defined by the IETF and other
internet authorities, by organizations, and other entities,
and their schemas are typically hosted as public
Show Menu documents on the web. They are identified by Uniform
3.5.6 Test-Driven Development (TDD)
Resource Names (URNs), used to make persistent
Software Development and documents reachable without the seeker needing to be
3 Design 
3.5.7 Lab - Create a Python Unit Test concerned about their location.

The code example below shows use of a namespace,


3.6 Understanding Data Formats  defined as the value of an xmlns attribute, to assert that
the content of an XML remote procedure call should be
3.6.1 Data Formats interpreted according to the legacy NETCONF 1.0
standard. This code-sample shows a NETCONF remote
procedure call instruction in XML. Attributes in the
3.6.2 XML
opening rpc tag denote the message ID and the XML
namespace that must be used to interpret the meaning of
3.6.3 JSON contained tags. In this case, you are asking that the
remote entity kill a particular session. The NETCONF XML
schema is documented by IETF.
3.6.4 YAML

<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

}
]

The problem is that XML has no way of deliberately


Show Menu
3.5.6 Test-Driven Development (TDD) indicating that a certain arrangement of tags and data
should be interpreted as a list. So we need to interpret
Software Development and
3 Design  the XML-writer's intention in making the translation.
3.5.7 Lab - Create a Python Unit Test
Mappings between XML tree structures and more-
efficient representations possible in various computer
languages require understanding data semantics. This is
3.6 Understanding Data Formats 
less true of more-modern data formats, like JSON and
YAML, which are structured to map well onto common
3.6.1 Data Formats simple and compound data types in popular programming
languages.
3.6.2 XML
In this case, the <vm> tags bracketing each instance's
data (id and type) are collapsed in favor of using plain
3.6.3 JSON brackets ( {} ) for grouping. This leaves you with a Python
list of objects (which you can call 'vm objects,' but which
3.6.4 YAML are not explicitly named in this declaration), each
containing two sub-objects, each containing a key/value
pair.
3.6.5 Parsing and Serializing

Lab - Parse Different Data Types


3.6.6
with Python

Software Development and


3.7
Design Summary  3.6.3

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.4 YAML {"keyname": "value"}

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 

JSON compound objects can be deeply-nested, with


Application Deployment and complex structure.
6 
Security
JSON can also express JavaScript ordered arrays (or
'lists') of data or objects. In the above example, the keys
Infrastructure and "some-integers" and "more-numbers" identify such
7 
Automation arrays.

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

Show Menu Whitespace in JSON is not significant, and files can be


3.5.6 Test-Driven Development (TDD)
indented using tabs or spaces as preferred, or not at all
Software Development and (which is a bad idea). This makes the format efficient
3 Design 
3.5.7 Lab - Create a Python Unit Test (whitespace can be removed without harming meaning)
and robust for use on command-lines and in other
scenarios where whitespace characters can easily be lost
3.6 Understanding Data Formats  in cut-and-paste operations.

3.6.1 Data Formats

3.6.4
3.6.2 XML
YAML 

3.6.3 JSON

YAML, an acronym for "YAML Ain't Markup Language", is


3.6.4 YAML
a superset of JSON designed for even easier human
readability. It is becoming more common as a format for
3.6.5 Parsing and Serializing configuration files, and particularly for writing declarative
automation templates for tools like Ansible.
Lab - Parse Different Data Types
3.6.6
with Python As a superset of JSON, YAML parsers can generally parse
JSON documents (but not vice-versa). Because of this,
Software Development and YAML is better than JSON at some tasks, including the
3.7
Design Summary 
ability to embed JSON directly (including quotes) in YAML
files. JSON can be embedded in JSON files too, but
quotes must be escaped with backslashes \" or
Understanding and Using
4  encoded as HTML character entities &quote;
APIs

Here is a version of the JSON file from the JSON


subsection, expressed in YAML. Use this as an example
5 Network Fundamentals 
to understand how YAML works:

Application Deployment and ---


6 
Security edit-config:
a-boolean: true
default-operation: merge
Infrastructure and
7  more-numbers:
Automation
- 225.0
- -1.0735
Cisco Platforms and some-integers:
8 
Development - 2
- 3
- 5
https://contenthub.netacad.com/devnet/3.6.1 9/16
7/23/24, 9:00 AM DevNet Associate -Understanding Data Formats

- 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.

3.6.2 XML YAML Data Types

YAML basic data types include numbers (written as


3.6.3 JSON positive and negative integers, as floats with a decimal, or
in scientific notation), strings, Booleans ( true and false ),
3.6.4 YAML or nulls (value left blank).

String values in YAML are often left unquoted. Quotes are


3.6.5 Parsing and Serializing only required when strings contain characters that have
meaning in YAML. For example, { ,a brace followed by a
Lab - Parse Different Data Types space, indicates the beginning of a map. Backslashes and
3.6.6
with Python other special characters or strings also need to be
considered. If you surround your text with double quotes,
Software Development and you can escape special characters in a string using
3.7
Design Summary 
backslash expressions, such as \n for newline.

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:

Cisco Platforms and


8 
Development my_integer: 2
my_float: 2.1
my_exponent: 2e+5
https://contenthub.netacad.com/devnet/3.6.1 10/16
7/23/24, 9:00 AM DevNet Associate -Understanding Data Formats

'my_complex:key' : "my quoted string


value\n"
0.2 : "can you believe that's a key?"
my_boolean: true
Show Menu my_null: null # might be interpreted as
3.5.6 Test-Driven Development (TDD)
empty string, otherwise
Software Development and
3 Design 
3.5.7 Lab - Create a Python Unit Test
YAML Indentation and File Structure

YAML does not use brackets or containing tag pairs, but


3.6 Understanding Data Formats 
instead indicates its hierarchy using indentation. Items
indented below a label are "members" of that labeled
3.6.1 Data Formats element.

3.6.2 XML The indentation amount is up to you. As little as a single


space can be used where indentation is required, though
a best-practice is to use two spaces per indent level. The
3.6.3 JSON important thing is to be absolutely consistent, and to use
spaces rather than tabs.
3.6.4 YAML
Maps and Lists

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:

Understanding and Using mymap:


4 
APIs myfirstkey: 5
mysecondkey: The quick brown fox

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

Cisco Platforms and


8  Maps and lists can also be represented in a so-called
Development
"flow syntax," which looks very much like JavaScript or
Python:

https://contenthub.netacad.com/devnet/3.6.1 11/16
7/23/24, 9:00 AM DevNet Associate -Understanding Data Formats

mymap: { myfirstkey: 5, mysecondkey: The


quick brown fox}
mylist: [1, 2, 3]

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.

3.6.2 XML mylongstring: >


This is my long string
which will end up with no linebreaks in it
3.6.3 JSON myotherlongstring: |
This is my other long string
3.6.4 YAML which will end up with linebreaks as in
the original

3.6.5 Parsing and Serializing


Note the difference in the two examples above. The
greater than ( > ) indicator gives us the folding syntax,
Lab - Parse Different Data Types
3.6.6 where the pipe ( | ) does not.
with Python

Software Development and Comments


3.7
Design Summary 
Comments in YAML can be inserted anywhere except in a
long string literal, and are preceded by the hash sign and
Understanding and Using a space:
4 
APIs

# 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

You would typically use a serialization function (from a


3.6.2 XML Python library) to output your auth data structure as a
string in XML format, adding it to your query:
3.6.3 JSON
import dicttoxml # serialization library
import requests # http request library
3.6.4 YAML
auth = { # Python dict,
containing authentication info
3.6.5 Parsing and Serializing "user": {
"username": "[email protected]",
Lab - Parse Different Data Types "key": "90823ff08409408aebcf4320384"
3.6.6
with Python }
}
Software Development and get_services_query =
3.7
Design Summary 
"https://myservice.com/status/services"
xmlstring = dicttoxml(auth) # convert
dict to XML in string form
Understanding and Using
4  myresponse =
APIs
requests.get(get_services_query,auth=xmlstring)
# query service

5 Network Fundamentals 

At this point, the service might reply, setting the variable


Application Deployment and myresponse to contain a string like the following,
6  containing service names and statuses in XML format:
Security

Infrastructure and <services>


7  <service>
Automation
<name>Service A</name>
<status>Running</status>
Cisco Platforms and </service>
8 
Development <service>
<name>Service B</name>
<status>Idle</status>
https://contenthub.netacad.com/devnet/3.6.1 14/16
7/23/24, 9:00 AM DevNet Associate -Understanding Data Formats

</service>
</services>

You would then need to parse the XML to extract


Show Menu
3.5.6 Test-Driven Development (TDD) information into a form that Python could access
conveniently.
Software Development and
3 Design 
3.5.7 Lab - Create a Python Unit Test
import untangle # xml parser library
myreponse_python =
3.6 Understanding Data Formats  untangle.parse(myresponse)
print
3.6.1 Data Formats myreponse_python.services.service[1].name.cdata

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 

Application Deployment and


6 
3.6.6
Security
Lab - Parse Different Data 

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:

Part 1: Launch the DEVASC VM


Part 2: Parse XML in Python
Show Menu Part 3: Parse JSON in Python
3.5.6 Test-Driven Development (TDD)
Part 4: Parse YAML in Python
Software Development and
3 Design   Parse Different Dat…
3.5.7 Lab - Create a Python Unit Test

3.6 Understanding Data Formats 

 
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

3.6.5 Parsing and Serializing

Lab - Parse Different Data Types


3.6.6
with Python

Software Development and


3.7
Design Summary 

Understanding and Using


4 
APIs

5 Network Fundamentals 

Application Deployment and


6 
Security

Infrastructure and
7 
Automation

Cisco Platforms and


8 
Development

https://contenthub.netacad.com/devnet/3.6.1 16/16

You might also like