No history yet

Introduction to JSON and XML

Structuring Data

Computers and applications are constantly talking to each other. When they do, they need a shared language to structure the information they exchange. Two of the most common languages for this are JSON and XML. Think of them as different ways to write down a grocery list so that anyone, or any application, can read it.

JSON (JavaScript Object Notation) is a lightweight, text-based data interchange format that's easy for humans to read and write, and easy for machines to parse and generate.

JSON organizes data using key-value pairs, much like a dictionary. The "key" is always a string in double quotes, and the "value" can be a string, a number, a true/false value, a list of items, or another object.

{
  "firstName": "Jane",
  "lastName": "Doe",
  "age": 30,
  "isStudent": false,
  "courses": [
    "History",
    "Math"
  ]
}

Data is enclosed in curly braces {} to form an "object." Lists of items, called "arrays," are enclosed in square brackets [].

Lesson image

XML, or eXtensible Markup Language, is another way to structure data. Instead of key-value pairs, it uses tags, similar to HTML.

<person>
  <firstName>Jane</firstName>
  <lastName>Doe</lastName>
  <age>30</age>
  <isStudent>false</isStudent>
  <courses>
    <course>History</course>
    <course>Math</course>
  </courses>
</person>

In XML, every piece of data is wrapped in opening and closing tags, like <firstName> and </firstName>. This structure is rigid and descriptive, making it very clear what each piece of data represents.

Lesson image

Key Differences

While both JSON and XML are used to store and transport data, they have some important differences. They are both human-readable and can represent nested, hierarchical data. But their syntax and typical use cases diverge.

FeatureJSONXML
SyntaxKey-value pairsOpening and closing tags
VerbosityMore conciseMore verbose
Data TypesSupports strings, numbers, booleans, arrays, objectsAll data is treated as a string
ParsingGenerally faster and easier for machinesCan be more complex to parse
Closing TagsNot requiredRequired for every opening tag

Because it's lightweight and maps directly to objects in many programming languages, JSON is extremely popular for web APIs, which allow different applications to communicate over the internet. XML is still common in many enterprise systems, configuration files, and for marking up documents where metadata is crucial.

Ready to check your understanding?

Quiz Questions 1/5

Which of the following snippets represents a valid JSON object?

Quiz Questions 2/5

True or False: Because it is lightweight and maps easily to objects in many programming languages, JSON is a popular choice for web APIs.

Both formats are powerful tools for structuring information. The choice between them often depends on the specific needs of the project, whether it's the speed and simplicity of JSON or the descriptive power of XML.