Intro to XML and JSON #7: Recap and Real World Use
Tags: xml, json, tutorial, beginners
Published: 2019-04-26 18:32:15

---
title: Intro to XML and JSON #7: Recap and Real World Use
description:  "Which is better?  When will I use these?"
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-7/
---

That’s a wrap! Let’s recap the battle of “XML vs. JSON,” discuss how they’re used in the real world, and take a final glance at the big picture.

After all, you might still be asking questions like:

- Which is better?
- When will I really use XML or JSON?

* * *

![](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 %}

* * *

![](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! [XML](http://codebeautify.org/xmlviewer), [JSON](http://codebeautify.org/jsonviewer) – paste & 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/catfight.jpg)

## XML vs. JSON

The advantages of each format stem from their unique traits.

- XML’s special talents 
  - Giving items names
  - Comments
  - Item IDs
- JSON’s special talents 
  - Not giving items names
  - Lists

* * *

![](https://katiekodes.com/images/catfight-lowercat.jpg)

### XML’s special talents

#### Giving items names

In data with a lot of internal variability, giving items names can make XML easy on human eyes.

For example, here’s what the first two levels of nested items in the XML behind the definition of my Salesforce “Contacts” table looks like right now _(only it’s 7,000 lines long)_:

```xml
<CustomObject>
	<actionOverrides>...(nested stuff)...</actionOverrides>
	<actionOverrides>...(nested stuff)...</actionOverrides>
	<actionOverrides>...(nested stuff)...</actionOverrides>
	<actionOverrides>...(nested stuff)...</actionOverrides>
	<compactLayoutAssignment>SYSTEM</compactLayoutAssignment>
	<enableFeeds>true</enableFeeds>
	<enableHistory>true</enableHistory>
	<fieldSets>...(nested stuff)...</fieldSets>
	<fieldSets>...(nested stuff)...</fieldSets>
	<fields>...(nested stuff)...</fields>
	<fields>...(nested stuff)...</fields>
	<fields>...(nested stuff)...</fields>
	<fields>...(nested stuff)...</fields>
	<fields>...(nested stuff)...</fields>
	<listViews>...(nested stuff)...</listViews>
	<searchLayouts>...(nested stuff)...</searchLayouts>
	<sharingModel>ReadWrite</sharingModel>
	<webLinks>...(nested stuff)...</webLinks>
</CustomObject>
```

Particularly when items at a given level are sorted in alphabetical order, there’s something **innately understandable** about this data.

I _like_ the way _**English words**_ dominate the structure, burying the details when I use software such as Notepad++ to “fold” the XML and view only one layer at a time.

I doubt it’d be so fast to determine which part of the structure controls the “custom fields” on this table in a sea of `{}`s and `[]`s where item “names” are just additional key-value pairs buried alongside the rest of an item’s data.

Here, it’s obvious that to add a new field to Salesforce, I’ll need to add a new one of those `<fields>...</fields>` thingies to the XML representing my table.

#### Comments

I forgot to cover it, but XML lets you use comments.

This XML:

```xml
<Dan Bday="Jan. 27">
	<!-- 
		This is a multi-line XML comment.
		The computer won't read any of this.
	-->
	<food>wine</food>
	<!--
		Nifty: I can "comment out" pickles 
		so that the computer won't think that Dan likes pickles, 
		but it will still be in this document 
		in case I change my mind later.
		See next line.
	-->
	<!--<food>pickles</food>-->
</Dan>
```

Is equivalent to this XML, as far as computers are concerned:

```xml
<Dan Bday="Jan. 27">
	<food>wine</food>
</Dan>
```

Of course, it’s _messy_ to leave “pickles” in your XML if Dan no longer likes pickles.

Now you have things in your text file that the human eye is going to see, like “pickles” and “nifty,” but that its brain has to remember to ignore.

Use comments sparingly and carefully, if at all.

#### Item IDs

I forgot to cover it, but when you use [the second _(“attributes”)_ approach to indicating them in XML](https://katiekodes.com/intro-xml-json-3/#keys--their-values---approach-2-attributes), anything you call “`ID`” _(case-insensitive)_ must meet a special rule for your XML to be valid:

You may not use the same value twice, _anywhere_ in the entire XML file.

This is extremely important for writing code that can “jump” to an item in an XML file.

For example, the HTML controlling this blog post includes the following element:

```html
<h2 id="posts-in-this-series">
	Posts In This Series
</h2>
```

Your web browser is coded to jump you directly to whichever piece of the HTML behind this article is ID’ed “`posts-in-this-series`” if you append “`#posts-in-this-series`” to the URL of this blog post.

That’s how I linked you to precise paragraphs of Marijn Haverbeke’s quotes about octopi when explaining JSON on objects.

[https://eloquentjavascript.net/2nd\_edition/04\_data.html](https://eloquentjavascript.net/2nd_edition/04_data.html) is a very large web page.

However, Marijn was very forward-thinking _(thank you!)_ and embedded an element called “`a`” at the beginning of each paragraph and gave it a unique ID, as below:

```html
<p>
	<a class="p_ident" id="p_FolCMJfte3" href="#p_FolCMJfte3"></a>
	To briefly return to our tentacle model of variable bindings—property bindings are similar.
	They <em>grasp</em> values, but other variables and properties might be holding onto those same values.
	You may think of objects as octopuses with any number of tentacles, each of which has a name inscribed on it.
</p>
```

Therefore, to provide links to individual paragraphs of his book, all I had to do was use my web browser to inspect the source code of […/04\_data.html](https://eloquentjavascript.net/2nd_edition/04_data.html), find “`p_FolCMJfte3`” attached to the paragraph that interested me, and link to […/04\_data.html#p\_FolCMJfte3](https://eloquentjavascript.net/2nd_edition/04_data.html#p_FolCMJfte3).

* * *

![](https://katiekodes.com/images/catfight-uppercat.jpg)

### JSON’s special talents

#### Not giving items names

When I [ask Salesforce Pardot’s API for JSON representing data in its “Prospects” table](https://katiekodes.com/python-pardot-null-clean/), the text Pardot sends me looks something like this:

```json
[
	{
		"id": 01010101, 
		"address_one": "123 Sunny St"
	}, 
	{
		"id": 02020202, 
		"address_one": "null"
	}
]
```

I **already know** that the whole file is full of records from the “Prospect” table, so I _like_ that my eyes and brain can lock in on **English words** representing **data** , not a bunch of repetitions of the word “prospect.”

#### Lists

Similarly to the benefits of “not giving items names,” JSON’s lists can reduce redundancy and store certain types of data very concisely.

This is particularly true when storing a list of simple values _(e.g. plaintext, numbers)_.

```json
[
	"wine",
	"pickles",
	"cream cheese",
	"herring",
	"tabbouli"
]
```

JSON’s list format doesn’t _always_ save space over XML, though. Listing back-to-back items could be shorter in either XML or JSON, depending on how long the items themselves are in each standard.

* * *

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

## Real World Usage: APIs

Let’s be honest: **you rarely get to choose how to write JSON or XML**.

Usually, you’re trying to download and process data from some sort of cloud product’s “[API](https://dev.to/katiekodes/salesforce-rest-apis-a-high-level-primer-2i9h)” – or upload data into one.

In that case, what matters most to you is being able to **recognize which kind** of data the “API” has given you, or expects from you.

After all, it’s not like you can get on the phone and argue with a company about which format they should have picked!

Hint:

- “ **REST** ” APIs typically want to receive data from you as **JSON**.
- “ **SOAP** ” APIs typically want to receive data from you as **XML**.
- Which type of data they **give** you is a little more variable.

In many of my [posts about APIs](https://katiekodes.com/tags#api), I talk about writing Python code or other code that can talk to cloud databases this way.

* * *

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

## Takeaways

1. XML and JSON are _standards_ for storing data as plain text that are quite similar, and that are optimized for storing “naturally list-shaped” data. 
  - However, they are sometimes used to store “naturally table-shaped” data when it’s desirable to avoid relying on line breaks to separate data.
2. CSV is a _standard_ for storing data as plain text that is optimized for storing “naturally table-shaped” data.
3. XML vs. JSON: XML is great with items that need names, comments, and giving each item a unique ID.
4. XML vs. JSON: JSON is great with items that don’t need names and values that can be listed.
5. XML vs. JSON: It doesn’t really matter which is great for which. You usually use both of them with other people’s software and have to live with their choices.

Thanks for sticking with the whole tutorial. Hopefully you’ve learned:

1. How to recognize “naturally table-shaped” vs. “naturally list-shaped” data, and how the difference between the two can impact attempts to convert data between CSV and XML/JSON formats.
2. Details that will help you read and write XML and JSON files comfortably.

As always, I’m excited to hear about your successes, of which I’m sure you’ll have many.

Be sure to let me know if you do any awesome projects with XML or JSON.