JSON, XML, and YAML: Understanding the Basics

PitsPits
8 min read

When working with data, you’ll often come across different formats that help store and share information between systems. Three of the most common ones are JSON, XML, and YAML. At first glance, they may look similar since all of them are used to organize and structure data, but each has its own style, strengths, and typical use cases.

In this blog, we’ll go through what these formats are, how they look, and why they are used. By the end, you’ll have a clear idea of the differences and when you might see one used over the other.


What is Data Serialization?

Before we dive into JSON, XML, and YAML, it’s important to understand the concept of data serialization.

Serialization simply means taking data from a program (like variables, objects, or records) and converting it into a format that can be easily stored or shared. For example, when two applications communicate over the internet, they can’t just send raw code or memory values. Instead, they need to send the data in a structured format that both sides understand. That’s where serialization comes in.

On the other hand, deserialization is the process of taking that structured data and turning it back into usable objects or variables in a program.

This is exactly why formats like JSON, XML, and YAML are widely used. They make it possible for data to move between different systems, applications, or even programming languages in a way that’s readable and consistent.


JSON (JavaScript Object Notation)

JSON is one of the most popular data serialization formats today. It is widely used in web applications, APIs, and configuration files. Its main appeal is that it is lightweight, easy to read, and easy to write.

Structure and Syntax

JSON represents data as key-value pairs. Keys are always strings, and values can be:

  • Strings

  • Numbers

  • Boolean values (true or false)

  • Arrays (lists of values)

  • Objects (nested key-value pairs)

  • null

Here’s a simple example:

{
  "name": "Alice",
  "age": 28,
  "isStudent": false,
  "skills": ["Python", "Networking", "Cloud"],
  "address": {
    "city": "Manila",
    "country": "Philippines"
  }
}

Notice how readable it is. The structure is clear, and even nested data like the address object is easy to follow.

Advantages of JSON

  • Human-readable: Easy to read and understand for both developers and non-developers.

  • Lightweight: Minimal syntax, which makes it faster to transmit over networks.

  • Language-independent: Most programming languages have built-in support for JSON.

  • Widely used in APIs: Many modern web services and applications rely on JSON for communication.

Disadvantages of JSON

  • Limited data types: It doesn’t support all data types natively, such as dates or binary data (you need to encode them).

  • No comments: You can’t add comments in JSON files, which can make documentation harder.

  • Less expressive than XML: For very complex structures, JSON might be less flexible than XML.

Common Use Cases

  • Exchanging data between web servers and clients (APIs)

  • Configuration files for applications

  • Storing structured data in databases like MongoDB


JSON and RFC 8259

JSON is not just a casual format; it has an official standard defined by RFC 8259. RFC stands for Request for Comments, which is how the Internet Engineering Task Force (IETF) publishes official specifications for internet technologies.

RFC 8259, published in December 2017, defines how JSON should be structured and interpreted. It clarifies rules such as:

  • JSON must be encoded in Unicode, usually UTF-8.

  • Objects are collections of key-value pairs, with keys as strings.

  • Arrays are ordered lists of values.

  • Values can be strings, numbers, objects, arrays, booleans, or null.

  • Whitespace is allowed but ignored outside of strings.

By following RFC 8259, developers ensure that JSON data is consistent and compatible across different platforms and programming languages.


JSON Example: show ip interface brief

Suppose the command show ip interface brief gives this output in a Cisco router:

Interface              IP-Address      OK? Method Status                Protocol
FastEthernet0/0        192.168.1.1     YES manual up                    up
FastEthernet0/1        unassigned      YES unset administratively down down

We can represent it in JSON like this:

{
  "interfaces": [
    {
      "name": "FastEthernet0/0",
      "ip_address": "192.168.1.1",
      "ok": "YES",
      "method": "manual",
      "status": "up",
      "protocol": "up"
    },
    {
      "name": "FastEthernet0/1",
      "ip_address": "unassigned",
      "ok": "YES",
      "method": "unset",
      "status": "administratively down",
      "protocol": "down"
    }
  ]
}

Why this is useful:

  • Makes the data easy to parse for scripts and network automation tools.

  • Allows applications to filter or manipulate interface information programmatically.

  • Standardizes outputs across different platforms.


JSON Data Types: Primitive and Structured

In JSON, values can be primitive or structured, which helps organize data clearly.

Primitive Types

Primitive types are the basic, simple values. JSON supports these primitive types:

  • String: Text enclosed in double quotes.

      "name": "Alice"
    
  • Number: Integers or decimals.

      "age": 28
    
  • Boolean: True or false values.

      "isStudent": false
    
  • Null: Represents an empty or missing value.

      "middleName": null
    

These types are called "primitive" because they are simple and cannot contain other values.

Structured Types

Structured types can contain multiple values, including other structured types:

  • Objects: Collections of key-value pairs.

      "address": {
        "city": "Manila",
        "country": "Philippines"
      }
    
  • Arrays: Ordered lists of values. These values can be primitive or objects.

      "skills": ["Python", "Networking", "Cloud"]
    

Structured types let us group related data together, which is why JSON can represent complex information in a readable way.


XML (eXtensible Markup Language)

XML is another popular format for storing and exchanging data. Unlike JSON, which uses braces and brackets, XML uses tags to define data. It has been around longer than JSON and is still widely used in enterprise systems, configuration files, and web services.

Structure and Syntax

XML organizes data using elements and attributes. An element is like a container for data, marked by opening and closing tags:

<interface>
  <name>FastEthernet0/0</name>
  <ip_address>192.168.1.1</ip_address>
  <status>up</status>
  <protocol>up</protocol>
</interface>

You can also include attributes within a tag:

<interface name="FastEthernet0/0" ip_address="192.168.1.1" status="up" protocol="up"/>

Both forms are valid. Attributes are useful for concise data, while elements are better for nested or complex information.

Primitive and Structured Data in XML

  • Primitive data in XML is the actual text inside an element or attribute. For example:

      <status>up</status>
    
  • Structured data comes from nesting elements. For example:

      <router>
        <interface>
          <name>FastEthernet0/0</name>
          <ip_address>192.168.1.1</ip_address>
        </interface>
        <interface>
          <name>FastEthernet0/1</name>
          <ip_address>unassigned</ip_address>
        </interface>
      </router>
    

Advantages of XML

  • Flexible: Can represent complex, hierarchical data.

  • Self-descriptive: Tags describe the data, which makes it readable.

  • Supports schemas: You can define rules for what data is allowed using DTD or XSD.

Disadvantages of XML

  • Verbose: More text and tags make files larger.

  • Harder to read: Compared to JSON, it can be more cluttered.

  • Slower to parse: More processing is needed due to extra tags.

Common Use Cases

  • Web services using SOAP

  • Configuration files in enterprise systems

  • Data exchange in legacy systems


YAML (YAML Ain’t Markup Language)

YAML is a data serialization format like JSON and XML, but it focuses on readability for humans. Its syntax is cleaner and less cluttered because it doesn’t use braces, brackets, or closing tags. YAML is often used in configuration files, automation scripts, and data exchange between systems.

Structure and Syntax

YAML uses indentation to represent hierarchy. Lists and key-value pairs are simple to read. Here’s an example representing show ip interface brief:

interfaces:
  - name: FastEthernet0/0
    ip_address: 192.168.1.1
    status: up
    protocol: up
  - name: FastEthernet0/1
    ip_address: unassigned
    status: administratively down
    protocol: down

Notice how the indentation and hyphens - define arrays and nested data, making it visually clean.

Primitive and Structured Data in YAML

  • Primitive types: Strings, numbers, booleans (true/false), and null values (null or ~).

      name: Alice
      age: 28
      isStudent: false
      middleName: null
    
  • Structured types:

    • Lists: Represented with hyphens.

        skills:
          - Python
          - Networking
          - Cloud
      
    • Objects/Maps: Represented using indentation.

        address:
          city: Manila
          country: Philippines
      

Advantages of YAML

  • Human-readable: Clean and easy to understand.

  • Less verbose: Compared to XML and even JSON.

  • Supports complex structures: Can handle nested data like JSON and XML.

  • Used in DevOps: Popular in tools like Ansible, Kubernetes, and Docker.

Disadvantages of YAML

  • Sensitive to indentation: Mistakes in spacing can cause errors.

  • Less widely supported than JSON: Not all programming languages have native YAML parsers.

  • No strict standard enforcement: Different parsers may interpret some features differently.

Common Use Cases

  • Application and service configuration files

  • Automation scripts (like Ansible playbooks)

  • Data exchange where human readability is a priority


JSON vs XML vs YAML: Quick Comparison

Here’s a simple table to compare the three formats:

FeatureJSONXMLYAML
SyntaxBraces {} and brackets []Tags <tag></tag>Indentation-based, uses - for lists
ReadabilityEasyModerateVery easy
Data TypesPrimitives + structuredPrimitives + structuredPrimitives + structured
VerbosityLowHighLow
CommentsNot supportedSupportedSupported
Human-friendlyYesModerateExcellent
Use CasesAPIs, web services, config filesEnterprise systems, legacy systems, web servicesConfig files, DevOps, automation scripts
Parsing & PerformanceFast, widely supportedSlower, supported everywhereModerate, requires parser library

Wrap-Up

In networking, tools like JSON, XML, and YAML are essential for automating tasks, managing configurations, and exchanging data between devices and systems.

  • JSON is lightweight and widely used in network APIs and automation scripts.

  • XML is flexible and still common in enterprise network systems, especially with older protocols or configuration files.

  • YAML is human-friendly and popular in network automation tools like Ansible, Kubernetes, and other DevOps solutions.

By understanding these formats, network engineers can read, write, and automate device configurations, parse outputs like show ip interface brief, and integrate network systems more efficiently. Choosing the right format makes network management faster, more reliable, and easier to scale.

0
Subscribe to my newsletter

Read articles from Pits directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pits
Pits