Intro to XML and JSON
Tags: xml, json, tutorial, beginners
Published: 2019-03-18 11:51:04

---
title: Intro to XML and JSON
published: true
tags: xml,json,tutorial,beginners
series: "Intro to XML and JSON"
cover_image: https://katiekodes.com/images/xml-json-intro-header-en.png
canonical_url: https://katiekodes.com/intro-xml-json-1/
---

XML, JSON, & CSV.  
  
All 3 are _incredibly_ common “plain-text” **file formats for transferring data between computers** , particularly over the internet _(e.g. over an “[API](https://dev.to/katiekodes/salesforce-rest-apis-a-high-level-primer-2i9h)“)_. But what’s the difference, and when should you use each one?  
  
_(Programmers, Salesforce business analysts, and database integration architects: you’ll want to know this!)_

![](https://katiekodes.com/images/grocery_vs_ledger.png)

## XML & JSON are for lists of lists. CSVs are for tables.

That’s a painful oversimplification, but I think it’s a good start if you’re completely new to the concepts.

* * *

## Core Principles

Let’s cover a few key principles about the XML & JSON file formats.

### Principle 1: XML ≈ JSON

XML & JSON are largely interchangeable with each other.

### Principle 2: XML/JSON \> CSV

XML & JSON are able to hold more complicated data than CSV files are designed to hold.

XML & JSON files are capable of storing any “table-shaped” data that a CSV file is capable of storing _(although typically not as concisely as a CSV file)_.

### Principle 3: CSV \< XML/JSON

Conversely, CSV files are a stricter, simpler file format.

CSV files cannot easily store all XML/JSON-suited data.

**Q:** Why not?

**A:**

1. CSV files are lousy at storing data where each “item” of data might have different “traits” than any other “item” in the dataset.
2. CSV files are lousy at “nesting” items inside of each other.

### Principle 4: Line Breaks ≠ Internet-Friendly

CSV files don’t transmit well over the internet in certain cases _(e.g. “HTTP requests”)_ because they have **_line breaks_** as part of the data format specification.

Line breaks don’t always transmit well from computer to computer.

- As mentioned, neither XML nor JSON are particularly concise for storing table-shaped data. _(JSON is a little “less bad.”)_

> If you need to see something to believe it, take a look at the output of Salesforce’s use of JSON instead of CSV when transmitting table data over the internet as follows:
> 
> * Log into the [Salesforce Developer Workbench’s REST Explorer](https://workbench.developerforce.com/restExplorer.php) with a Salesforce dev org that has data in the “Account” table.
> * Leave the HTTP method as “GET”
> * Fill in the box with: 
```
/services/data/v44.0/query/?q=SELECT+Name+FROM+Account+LIMIT+5
```
> * Click “Execute.”
> * Click “Show Raw Response.”
> 
> The data in the blue box on the right-hand side of the screen is formatted in the “JSON” syntax!

* * *

## Material

It’s easy to learn to write CSV files – see their [Wikipedia article](https://en.wikipedia.org/wiki/Comma-separated_values).

Therefore, this series is going to focus on teaching you to read and write **XML** and **JSON** files at a **conceptual** level.

You’ll learn:

1. Ways that subtle differences between the XML & JSON standards influence programmers’ preferences. 
  - _(Although they often choose a standard because “that’s what my coworker likes” – which is [not actually a bad reason](https://en.wikipedia.org/wiki/Coding_conventions)!)_
2. To recognize when data formatted as XML or JSON is just serving as an “internet-friendly” wrapper around “table-shaped,” CSV-like data. 
  - In these cases, tools exist to help you transform the data into a spreadsheet _(e.g. the Python programming language’s “Pandas” module)_
3. The structural details of both XML and JSON formats, so that editing them won’t be scary.

I recommend reading the whole series, because I start with XML, then take a somewhat “compare-and-contrast” approach while explaining JSON to avoid repeating myself.

* * *

## Pay attention to “shape!”

But before we begin, please be sure to remember this as you read:

1. If your data fits naturally in an **Excel spreadsheet** , it’s probably “ **table-shaped** ” and **CSV** is probably its most natural format. 
  - Nevertheless, you can use XML or JSON _(JSON will be more concise)_, if you need to avoid line breaks.
2. If your data fits naturally in a “shopping list” or “ **nested bullet points** ” format, it’s probably **“list of list”-shaped** and **XML/JSON** is probably its most natural format.

* * *

![](https://katiekodes.com/images/bouquet-wide.jpg)

## Viewing “Pretty” XML & JSON

We’ll have a lot of examples in this series.

I recommend that you edit them and play with seeing them in a “pretty” format!

- To view my XML examples graphically, paste them [here](http://codebeautify.org/xmlviewer) and click “Tree View”.
- To view my JSON examples graphically, paste them [here](http://codebeautify.org/jsonviewer) and click “Tree View”.

Warning: only put sample data into the “beautifier” links above. **Never** put your company’s confidential data into a stranger’s web site.

* * *

![](https://katiekodes.com/images/booksmany.jpg)

## Posts In This Series

1. Overview {% link https://dev.to/katiekodes/intro-to-xml-and-json-56co %}
2. Tables vs. Lists _(this post)_ {% link https://dev.to/katiekodes/intro-to-xml-and-json-2-datas-shape-3iji %}
3. XML 1 {% link https://dev.to/katiekodes/intro-to-xml-and-json-3-xml-items-keys-1m7i %}
4. XML 2 {% link https://dev.to/katiekodes/dying-intro-to-xml-and-json-4-xml-values-5bi1 %}
5. XML 3 {% link https://dev.to/katiekodes/intro-to-xml-and-json-5-xml-csv-conversions-45n5 %}
6. JSON {% link https://dev.to/katiekodes/intro-to-xml-and-json-6-json-30h3 %}
7. Recap {% link https://dev.to/katiekodes/intro-to-xml-and-json-7-recap-and-real-world-use-5586 %}