Software Development and Design · Topic 1 of 7

Comparing XML, JSON and YAML

Compare the XML, JSON and YAML data formats, see the same data written in each, and learn where network automation uses them and how to tell them apart.

  • 4 min
In a nutshell
  • JSON uses braces, brackets and double quoted keys, and does not allow comments.
  • YAML uses indentation and dashes, allows comments, and forbids tabs for indentation.
  • XML uses start and end tags, allows attributes and comments, and has one root element.

What it is

A data format is an agreed way to write structured data as text so that two programs can exchange it.

Network automation moves a lot of data between tools, APIs and devices. XML, JSON and YAML are the three formats you must recognise on sight.

All three describe the same kinds of thing: named values, lists of items, and objects nested inside other objects. They differ in how they look and where they are used.

Python scriptbuilds a dictserialiseTextJSON, XML or YAMLsent overNetworkAPI or fileparseOther programreads the data

How it works

Each format marks structure in its own way, so the skill is spotting the pattern quickly.

The same data in JSON

JSON uses braces for objects, square brackets for lists, and double quotes around every key and every string.

{
  "hostname": "R1",
  "enabled": true,
  "interfaces": [
    {"name": "Gi0/0", "ip": "10.0.0.1"},
    {"name": "Gi0/1", "ip": "10.0.1.1"}
  ],
  "uptime_days": 42
}

Values can be strings, numbers, true, false, null, lists or objects. JSON does not allow comments and does not allow a trailing comma after the last item.

The same data in YAML

YAML shows structure with indentation. Lists use a dash at the start of each item, and quotes are usually optional.

hostname: R1
enabled: true
interfaces:
  - name: Gi0/0
    ip: 10.0.0.1
  - name: Gi0/1
    ip: 10.0.1.1
uptime_days: 42

YAML allows comments that start with a hash sign. It must be indented with spaces, never with tabs. Ansible playbooks, many CI/CD files and Docker Compose files use YAML.

The same data in XML

XML wraps every value in a start tag and a matching end tag, written with angle brackets. A hostname element would hold the text R1 between its opening and closing tags.

Elements can be nested, and an element can also carry attributes inside its start tag. A document has exactly one root element. XML allows comments and has a strict, well defined structure.

NETCONF uses XML, and so do some older APIs such as the Cisco UCS Manager XML API and the AXL interface of Unified Communications Manager.

Side by side

FeatureJSONYAMLXML
Structure marked byBraces and bracketsIndentationStart and end tags
CommentsNoYesYes
Human readabilityGoodBestWordy
Typical useREST APIs, RESTCONFConfig files, AnsibleNETCONF, SOAP
Data typesStrings, numbers, booleans, null, lists, objectsSame, plus moreEverything is text unless a schema says otherwise

Telling them apart quickly

  • Curly braces with quoted keys point to JSON.
  • Lines of key: value with indentation and dashes point to YAML.
  • Angle brackets with closing tags point to XML.

JSON is a subset of what YAML can express, so a valid JSON file can usually be read by a YAML parser. The reverse is not true.

Why REST APIs favour JSON

JSON maps almost directly to Python dictionaries and lists. It is compact and every language has a parser for it.

An API client usually asks for a format with the Accept header and states the format it sends with the Content-Type header.

Accept: application/json
Content-Type: application/json

The media type for XML is application/xml. YAML is rarely used on the wire, so you will mostly see it in files.

Exam tip

If a question shows a block of data, count the clues. Braces and quotes mean JSON, dashes and colons without braces mean YAML, and tags mean XML.

What to remember for the exam

Seven facts cover this topic.

  • JSON uses braces, brackets and double quoted keys, and does not allow comments.
  • YAML uses indentation and dashes, allows comments, and forbids tabs for indentation.
  • XML uses start and end tags, allows attributes and comments, and has one root element.
  • REST APIs mostly use JSON. NETCONF uses XML.
  • Ansible and many CI/CD tools read YAML.
  • application/json and application/xml are the usual media types.
  • All three represent nested objects and lists, so the same data can move between them.

Common mistakes

Four errors appear often.

Trailing commas in JSON. A comma after the last item makes the document invalid. Python dictionaries allow it, which is why it slips in.

Single quotes in JSON. JSON needs double quotes for keys and strings. Single quotes are a syntax error.

Tabs in YAML. Indentation must use spaces, and mixed indentation changes the meaning or breaks the file.

Assuming XML is just JSON with tags. XML has attributes, a single root and text only values, so a direct conversion needs choices.

Watch out

A YAML value such as no or on may be read as a boolean by some parsers. Quote values when you want them kept as text.

Check yourself

Work through these three questions.

  • Which format uses indentation and dashes to show structure and allows comments?

    YAML does. It must be indented with spaces, not tabs.

  • Why do most REST APIs use JSON?

    It is compact, easy to read and maps almost directly to Python dictionaries and lists.

  • What is wrong with a JSON document that ends a list with a comma after the last item?

    A trailing comma is not allowed in JSON, so the document is invalid.

Topic 1 of 7 in Software Development and Design

Test yourself on Software Development and Design

The full Cisco CCNA 200-901 DEVASC question bank has 808 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by Cisco. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.