JSON vs XML
JSON vs XML — Quick Summary
| JSON | XML | |
|---|---|---|
| Full name | JavaScript Object Notation | eXtensible Markup Language |
| File size | Smaller — no closing tags | Larger — opening + closing tags |
| Human readable | Yes — minimal syntax | Verbose but structured |
| Data types | string, number, boolean, null, object, array | All values are strings (type via XSD) |
| Comments | Not allowed | Supported (<! -- ... -->) |
| Attributes | Not a concept | Yes — elements can have attributes |
| Namespaces | Not a concept | Yes — xmlns prefix |
| Schema | JSON Schema | XSD (XML Schema Definition) |
| Query language | JSONPath | XPath / XQuery |
| Transform | No standard | XSLT |
| Browser support | JSON.parse() built-in | DOMParser built-in |
| Primary use | REST APIs, config files, databases | SOAP APIs, enterprise, document storage |
Side-by-Side Example
The same user object represented in both formats:
json
// JSON — 181 characters
{
"user": {
"id": 42,
"name": "Ravi Kumar",
"email": "ravi@example.com",
"active": true,
"tags": ["developer", "golang"]
}
}xml
<!-- XML — 281 characters -->
<?xml version="1.0" encoding="UTF-8"?>
<user>
<id>42</id>
<name>Ravi Kumar</name>
<email>ravi@example.com</email>
<active>true</active>
<tags>
<tag>developer</tag>
<tag>golang</tag>
</tags>
</user>Same data. JSON is 35% smaller in this example. For repeated arrays of records the difference compounds.
Parsing in Go
go
// JSON parsing
import "encoding/json"
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Active bool `json:"active"`
Tags []string `json:"tags"`
}
var user User
err := json.Unmarshal([]byte(jsonStr), &user)
// XML parsing
import "encoding/xml"
type UserXML struct {
XMLName xml.Name `xml:"user"`
ID int `xml:"id"`
Name string `xml:"name"`
Email string `xml:"email"`
Active string `xml:"active"` // always string in XML
Tags struct {
Tag []string `xml:"tag"`
} `xml:"tags"`
}
var userXML UserXML
err = xml.Unmarshal([]byte(xmlStr), &userXML)When to Use JSON
- REST APIs — JSON is the de facto standard. Every HTTP framework has built-in JSON support.
- Configuration files — package.json, tsconfig.json, .eslintrc. Smaller and more readable than XML config.
- NoSQL databases — MongoDB, DynamoDB, Firestore all store documents as JSON/BSON.
- Browser-to-server communication — JSON.parse() and JSON.stringify() are built into every browser.
- Microservices — Lightweight and fast to parse. Easy to generate in any language.
When XML Still Wins
- SOAP web services — legacy enterprise systems and banks often require SOAP, which is XML-based.
- Document-centric data — mixed content (text + markup tags inline), like HTML itself or rich text documents.
- Complex namespacing — XML namespaces allow multiple schemas to coexist in one document.
- XSLT transformations — you need to transform documents into HTML or other formats using XSLT.
- Strict schema validation — XSD is more expressive than JSON Schema for certain data constraints.