Concepts / XML Basics and Structure

XML Basics and Structure

XML uses tags and attributes; JSON uses curly braces and key-value pairs to represent the same information.

  • Programming

One Person, Two Formats

When two programs share information, they need a common format. XML and JSON can both represent the same information, but they organize it differently. XML uses nested tags and attributes, while JSON uses curly braces and key-value pairs. The important difference is not merely how the formats look: JSON is designed around data structures that programs already use, which makes it especially convenient for communication between programs.

What do you think happens?

A phone number has extra information indicating whether it is international. In XML, where would that extra information most naturally appear?

  • Inside the opening phone tag
  • Only in a separate root tag
  • As a curly brace around the whole document
Reveal answer

Answer: Inside the opening phone tag

XML attaches additional information as an attribute inside an element's opening tag. JSON does not have a separate attribute concept; the same information becomes another key-value pair.

XML's Nested Elements

XML structures information with nested tags. Each piece of information lives inside an element, represented by an opening tag and a closing tag. A person element can contain child elements such as name, phone, and email. This creates a hierarchy: person contains the other elements, and those elements contain their values.

xml

In this example, person is the outer element. The name, phone, and email elements are nested inside it. The value true is not the main content of phone; it is metadata attached to phone through the intl attribute in the opening tag. This separation between an element's main content and its attributes is a central part of XML's design.

containscontainsattributecontainspersonouter elementnameRiyaintltruephone555-0100emailriya@example.test
What contains what in this XML document, and where does the extra phone information belong?

The Same Data in JSON

Converting the person representation

Represent the person information from the XML example in JSON.

Replace the outer element: The XML person tag is represented by an outer JSON object delimited by curly braces.

Convert child elements: The name, phone, and email elements become JSON key-value pairs.

Flatten the attribute: The XML intl attribute becomes another ordinary key-value pair named phone_intl. JSON has no separate attribute concept.

The information can be represented as an object containing name, phone, email, and phone_intl.

json

The XML person tag is not given a separate JSON tag. Instead, the outer curly braces represent the object as a whole. The intl attribute is also not kept as a special kind of property attached to phone. It becomes phone_intl, an ordinary key at the same level as phone. This is a deliberate simplification: JSON treats the information as key-value pairs rather than separating element content from attributes.

containshaskey-valuekey-valuepersonnested elementphoneelement contentobjectcurly bracesphone555-0100intlattributephone_intltrue
How is the same person information represented differently through XML tags and attributes versus JSON curly braces and key-value pairs?

Why JSON Maps Easily to Programs

JSON's main structures are objects and arrays. These correspond closely to data structures found in programming languages: dictionaries, hash maps, objects, or associative arrays for key-value data, and lists or arrays for ordered collections. Nearly all programming languages have equivalents of dictionaries and lists, so JSON can act as a shared bridge between programs.

maps tomaps tomaps tomaps toJSON objectcurly bracesdictionarykey-value datalistordered valuesobjectkey-value dataJSON arraysquare bracketsarrayordered values
How do JSON objects and arrays map to common programming structures?

When a program receives JSON, it can parse the data into structures that are already familiar to the language. The source explains that a Python program can receive JSON as a Python dictionary, while a JavaScript program can parse it into a JavaScript object. This direct mapping avoids the need to build a custom object model before the program can work with the data.

From One Program to Another

Suppose Program A creates data using its native dictionary-like structure. It can serialize that data into JSON and send the JSON to Program B over a network. Program B can parse the JSON into its own native object-like structure. Because both programs understand the shared JSON format, they can exchange the data even when they are written in different languages.

serializesendparseProgram AdictionaryJSONshared formatnetworkdata transferProgram Bobject
How does structured data move between programs when JSON is used as the shared exchange format?
QuestionXMLJSON
Main structureNested tags and attributesObjects and key-value pairs
Extra informationCan be an attribute in an opening tagBecomes another key-value pair
Programming-language relationshipNeeds parsing of XML-specific concepts and may need further conversionMaps directly to dictionaries, objects, lists, or arrays
Typical strengthComplex documents and formats needing extensive metadataSimple data exchange between programs

Mistakes Beginners Make

  • Treating an XML attribute as if it were a nested child element.

    XML distinguishes the main content of an element from metadata attached as an attribute.

    Fix: Look inside the opening tag for attributes, and look between opening and closing tags for the element's main content.

  • Looking for a separate attribute category in JSON.

    JSON deliberately removes the separate tag-and-attribute model.

    Fix: Represent the extra information as an ordinary key-value pair.

  • Assuming XML and JSON have the same structural vocabulary.

    Curly braces define a JSON object, while XML uses opening and closing tags to define elements.

    Fix: Use the vocabulary of the format: elements, tags, and attributes for XML; objects, arrays, keys, and values for JSON.

  • Choosing XML or JSON only because one looks familiar.

    The choice should reflect the data and the systems exchanging it.

    Fix: Prefer JSON when direct mapping to common programming structures and simple exchange are the main goals; use XML when an existing system or advanced metadata needs calls for it.

Check Your Understanding

MEDIUM

A record contains a name, an email address, and a phone number with extra information about whether the number is international. Describe how XML would organize this record and how JSON would represent the same extra information. Then explain why a program may find the JSON version easier to use after parsing.

Hints
  • Identify the outer XML element and its nested child elements.
  • Place the extra phone information in an XML attribute.
  • In JSON, turn the extra information into an ordinary key-value pair.
  • Connect JSON objects with dictionaries or objects in programming languages.

A quick structural diagnosis

A format uses nested tags, and extra details appear inside an opening tag. Is it using the XML approach or the JSON approach?

Identify the markers: Nested tags and opening tags are XML concepts.

Identify the metadata model: Extra details inside an opening tag are attributes, another XML concept.

Contrast with JSON: JSON uses curly-braced objects and represents the extra detail as another key-value pair.

The format is using the XML approach.

The Practical Rule

XML and JSON can carry the same information, but they express structure differently. XML uses a hierarchy of nested elements and distinguishes element content from attributes. JSON uses objects, arrays, and key-value pairs, turning attributes into ordinary keys. Because those structures resemble dictionaries, objects, lists, and arrays found in nearly all programming languages, JSON usually requires less translation when programs exchange data.

Key Takeaways

  • XML organizes information with nested tags and can attach metadata through attributes in opening tags.
  • JSON represents information with objects, arrays, and key-value pairs, without a separate attribute concept.
  • JSON objects and arrays map closely to dictionaries, objects, lists, and arrays in programming languages.
  • JSON's smaller set of structural concepts reduces parsing complexity and learning overhead.
  • JSON is commonly preferred for new program-to-program data exchange, while XML remains useful for complex metadata, document formats, and legacy systems.