JSON Syntax and Structure
XML uses tags and attributes; JSON uses curly braces and key-value pairs to represent the same information.
Two Ways to Represent Information
When two programs exchange information, they must agree on a shared format. XML and JSON can both represent the same information, but they organize that information differently. XML emphasizes nested tags and attributes. JSON emphasizes objects made from key-value pairs and collections represented by arrays.
The important difference is not merely how the formats look. JSON is designed around structures that resemble the dictionaries, maps, objects, lists, and arrays already used by many programming languages. That design makes JSON especially useful when programs need to exchange data.
What do you think happens?
A phone number has an additional detail indicating whether it is international. In XML, where would that detail normally appear, and how would JSON represent it?
Reveal answer
Answer: XML uses an attribute; JSON uses another key-value pair
XML separates element content from extra information by using attributes inside an opening tag. JSON does not have a separate attribute concept, so the extra information becomes an ordinary key-value pair.
Tags, Attributes, and Key-Value Pairs
XML uses nested tags to structure information. Each piece of information is placed inside an element such as person, name, phone, or email. XML can also attach extra information to an element through an attribute in the opening tag. For example, an intl attribute can indicate whether a phone number is international.
JSON removes the distinction between an element's main content and its attributes. A JSON object is wrapped in curly braces and contains key-value pairs. Information that XML might store as an attribute can become another key at the same level in the JSON object. The attribute concept is therefore flattened into the same key-value structure used for the rest of the data.
| Structural question | XML | JSON |
|---|---|---|
| How is the main structure marked? | Nested opening and closing tags | Curly braces around an object |
| How is a piece of information named? | An element tag | A key in a key-value pair |
| How is extra information attached? | An attribute inside an opening tag | Another ordinary key-value pair |
| How are collections represented? | Nested elements | Arrays using square brackets |
The formats can represent similar information while using different structural concepts.
From Nested Data to Native Structures
JSON's main structures are objects and arrays. Objects use curly braces and key-value pairs. Arrays use square brackets to represent list-like collections. JSON also supports strings, numbers, booleans, and null. Together, these provide a small and consistent set of building blocks for nested data.
These building blocks map closely to structures found in programming languages. A JSON object can correspond to a dictionary, map, object, or associative array. A JSON array can correspond to a list or array. Because nearly all programming languages have equivalents for dictionaries and lists, JSON can act as a common bridge between programs.
Mapping a Nested Record
Consider a JSON object containing a person's name, phone number, phone_intl detail, and a list of contact methods. What kinds of programming language structures does this shape suggest?
Outer structure: The curly braces indicate an object. A program can treat the outer structure as a dictionary, map, or object.
Named information: Each name, phone, phone_intl, or contact-method entry is represented as a key-value pair inside the object.
Collection: A group of contact methods represented with square brackets is an array in JSON and can map to a list or array in a programming language.
Nested use: Because objects and arrays can be nested, a program can work with a structure that combines a map-like outer value and list-like inner values.
The JSON shape can be used by a program as familiar native structures instead of requiring a separate custom representation for every JSON concept.
The Program-to-Program Exchange
Suppose Program A creates data in a dictionary-like structure and needs to send it to Program B. Program A serializes that data into JSON and sends the JSON across the network. Program B receives the JSON and parses it into a native object or another key-value structure. The shared JSON shape gives both programs a format they can read and write.
This exchange can work even when the programs are written in different languages. For example, the source describes a Python program creating a dictionary and a JavaScript program parsing the received JSON into a JavaScript object. JSON's objects and arrays resemble structures available in both languages, so the receiving program does not need to construct a completely different intermediate model first.
XML can also serve as an exchange format, but its parser must understand tags, attributes, nested hierarchies, and other XML-specific concepts. After parsing, the data may still need to be converted into the receiving language's native structures. JSON's smaller conceptual model reduces this additional translation work for common program-to-program data exchange.
When evaluating an exchange format, look at the receiving program as well as the message itself. If the data naturally consists of objects, key-value pairs, and lists, JSON's structure can make the boundary between the exchanged data and the program's own data structures straightforward.
Why Simplicity Matters
JSON is simpler than XML because it has fewer syntactic concepts. Its design focuses on objects, arrays, strings, numbers, booleans, and null. A learner or parser can reason about nested objects and arrays without separately tracking whether information is an element, an attribute, or another XML-specific feature.
XML has capabilities that JSON deliberately omits, including complex metadata through attributes, namespaces, processing instructions, and other advanced features. Those capabilities can be useful, especially in complex document formats, configuration files requiring extensive metadata, and legacy systems. They also add concepts that a parser and learner must handle.
| Concern | JSON | XML |
|---|---|---|
| Primary structural model | Objects, key-value pairs, and arrays | Nested tags and attributes |
| Relationship to programming structures | Directly resembles dictionaries, maps, objects, lists, and arrays | Often requires parsing XML concepts and then converting the result |
| Conceptual size | Fewer syntactic concepts | More capabilities and XML-specific concepts |
| Typical strength in this comparison | Straightforward program-to-program exchange | Complex documents, extensive metadata, and existing XML systems |
Common Structural Mistakes
Treating JSON attributes as a separate syntax category
JSON does not have a separate attribute concept. Information that XML might express as an attribute becomes another key-value pair.
Fix:
Think of phone and phone_intl as keys at the same object level when the JSON structure represents them that way.Assuming that JSON objects and arrays are interchangeable
Objects represent key-value relationships, while arrays represent list-like collections.
Fix:
Use the curly-brace object model for named keys and the square-bracket array model for collections.Thinking JSON is useful only because it is shorter to read
The deeper reason is that JSON maps directly to common programming language structures and therefore reduces translation complexity.
Fix:
Evaluate how the exchanged structure will map into the sender's and receiver's native data structures.Assuming XML is never appropriate
XML remains useful for complex document formats, configuration files requiring extensive metadata, and legacy systems.
Fix:
Prefer JSON for straightforward new program-to-program exchange, but use XML when an existing system or XML's advanced features make it suitable.
Choosing Between JSON and XML
For a new system that needs straightforward data exchange between a browser, server, mobile application, or other programs, JSON is usually the practical default described in this topic. Its objects and arrays align with structures that most programming languages already provide, and its simpler syntax reduces parsing and learning overhead.
XML can be the better choice when the system already depends on XML, when the data belongs to a complex document format, or when extensive metadata and XML-specific capabilities are needed. The choice is therefore based on the shape and requirements of the information exchange, not on the claim that one format can represent data and the other cannot.
A team is designing a new exchange format for a browser and a server. The data contains named fields and lists, and both programs already use dictionary-like and list-like structures. Explain why JSON is a strong candidate. Then describe one situation in which XML might still be selected.
Hints
- Relate JSON objects to dictionaries, maps, or objects.
- Relate JSON arrays to lists or arrays.
- Mention reduced parsing and translation complexity.
- For XML, use an existing XML-based system or a need for extensive metadata.
Key Takeaways
- XML uses nested tags and attributes, while JSON uses objects with key-value pairs and arrays.
- Information that XML might represent as an attribute can become an ordinary JSON key-value pair.
- JSON objects and arrays map directly to dictionaries, maps, objects, lists, and arrays in many programming languages.
- This direct mapping helps programs written in different languages exchange data with less translation work.
- JSON is usually preferred for straightforward modern data exchange, while XML remains useful for complex or legacy systems.
Key Takeaways
- XML organizes information with nested tags and attributes; JSON organizes it with objects, key-value pairs, and arrays.
- JSON deliberately treats extra information as ordinary key-value data rather than creating a separate attribute concept.
- JSON's structures resemble native dictionaries, maps, objects, lists, and arrays found across programming languages.
- This resemblance makes JSON a practical bridge when different programs exchange data.
- JSON's simplicity suits straightforward data exchange, while XML remains appropriate for complex, metadata-heavy, or legacy systems.