# Comparation

### What are the similarities between JSON and XML? <a href="#seo-faq-pairs-what-are-the-similarities-between-json-and-xml" id="seo-faq-pairs-what-are-the-similarities-between-json-and-xml"></a>

JSON and XML are both data serialization formats. They allow you to exchange data across different applications, platforms, or systems in a standardized manner.

Different programming languages and platforms represent the same data differently. For example, a Java application uses a data object, and a Python application uses a dictionary to store information about the same real-world entity. To exchange data between them, you can take the following steps:

1. Convert the Java object to XML or JSON
2. Transmit the XML or JSON file over a network
3. Reconvert the XML or JSON to Python dictionary

![](https://d2908q01vomqb2.cloudfront.net/f1f836cb4ea6efb2a0b1b99f41ad8b103eff4b59/2021/09/10/2037-Architecture-Update.jpg)

Conversions are built into language libraries, and this allows programmers to write applications that communicate with each other by using APIs. Additionally, both formats are self-describing, so you can read and edit JSON and XML files in any text editor.

Although XML is an older technology, both JSON and XML are still commonly used.

### Data representation: JSON vs. XML <a href="#seo-faq-pairs-data-representation-json-vs-xml" id="seo-faq-pairs-data-representation-json-vs-xml"></a>

XML represents data in a tree pattern, while JSON uses key-value pairs. The following examples display the same information in both data representations.&#x20;

#### **Example: JSON document**

The following example displays the names of three guests in JSON.

```json
{"guests":[
  { "firstName": "John", "lastName": "Doe" },
  { "firstName":"María", "lastName": "García" },
  { "firstName": "Nikki", "lastName": "Wolf" }
]}
```

#### **Example: XML document**

The following example displays the names of three guests in XML.

```xml
<guests>
  <guest>
    <firstName>John</firstName> <lastName>Doe</lastName>
  </guest>
  <guest>
    <firstName>María</firstName> <lastName>García</lastName>
  </guest>
  <guest>
    <firstName>Nikki</firstName> <lastName>Wolf</lastName>
  </guest>
</guests>
```

### Key differences: JSON vs. XML <a href="#seo-faq-pairs-key-differences-json-vs-xml" id="seo-faq-pairs-key-differences-json-vs-xml"></a>

Although JSON and XML serve similar purposes, some core differences set them apart. Understanding these differences will help you to decide which is more helpful for your use case.

#### **History**

The XML Working Group conceived XML in 1996 and released its initial version in 1998. They derived XML from the Standard Generalized Markup Language (SGML). After introducing HTML in 1998, they developed XML as a data serialization tool.

Douglas Crockford and Chip Morningstar released JSON in 2001. They derived JSON from JavaScript.

#### **Format**

JSON uses key-value pairs to create a maplike structure. The key is a string, which will identify the pair. The value is the information that you give to that key. For example, we could have _“NumberProperty”: 10_. In this, _“NumberProperty”_ is the key, and _10_ is the value.

In contrast, XML is a markup language—a subset of SGML with a structure similar to HTML. It stores data in a tree structure that presents layers of information that you can follow and read. The tree begins with a root (parent) element before giving information about child elements. This expansive structure is helpful for loading in lots of variables and dynamic configurations.&#x20;

#### **Syntax**

The syntax used in JSON is more compact and easier to write and read. It allows you to define objects easily.

XML is more verbose and substitutes certain characters for entity references. For example, instead of the _<_ character, XML uses the entity reference _\&it;_. XML also uses end tags, which makes it longer than JSON.

#### **Parsing**

You must parse XML with an XML parser, which often slows and complicates the process.

You can parse JSON by a standard JavaScript function, which is more accessible. Because of their syntax and file size differences, you can also parse JSON faster than XML.

#### **Schema documentation**

Schema documentation describes the purpose of a file, showing what you should use it for.

XML documents have a link to their schema in the header. The schema is also in XML format, which allows you to read what you should expect to find in the file. You can then validate the document against the schema and check that everything has loaded correctly and without errors.

JSON also allows you to use schemas. However, they’re simpler and allow greater flexibility.

#### **Data type support**

JSON only supports a limited range of data types like strings, numbers, and objects. JSON can also support Boolean arrays, which XML cannot do without adding additional tags.

However, XML is more flexible and supports complex data types like binary data and timestamps.&#x20;

#### **Ease of use**

As a markup language, XML is more complex and requires a tag structure.

In contrast, JSON is a data format that extends from JavaScript. It does not use tags, which makes it more compact and easier to read for humans. JSON can represent the same data in a smaller file size for faster data transfer.

#### **Security**

JSON parsing is safer than XML.

The structure of XML is vulnerable to unauthorized modifications, which creates a security risk known as XML external entity injection (XXE). It’s also vulnerable to unstructured external document type declaration (DTD). You can prevent both of these issues by turning off the DTD feature in transmission.&#x20;

### When to use: JSON vs. XML <a href="#seo-faq-pairs-when-to-use-json-vs-xml" id="seo-faq-pairs-when-to-use-json-vs-xml"></a>

If you want to store several different data types with many variables, then XML is the better choice. XML checks for errors in complex data more efficiently than JSON, as XML focuses on storing data in a machine-readable way. It also has a more mature set of tools and libraries and may work better with legacy systems.

![](https://d2908q01vomqb2.cloudfront.net/b6692ea5df920cad691c20319a6fffd7a4a766b8/2020/07/30/StreamTransformAnalyzeKinesisLambdaRedshift1.png)

On the other hand, JSON was designed for data interchange and provides a simpler and more concise format. It also improves performance and communication speed.

JSON is generally a better choice for APIs, mobile apps, and data storage, while XML is better suited for complex document structures that require data exchange.

### Summary of differences: JSON vs. XML <a href="#seo-faq-pairs-summary-of-differences-json-vs-xml" id="seo-faq-pairs-summary-of-differences-json-vs-xml"></a>

|                          | JSON                                                             | XML                                                                                                |
| ------------------------ | ---------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- |
| **Stands for**           | _JSON_ means JavaScript Object Notation.                         | _XML_ means Extensible Markup Language.                                                            |
| **History**              | Douglas Crockford and Chip Morningstar released JSON in 2001.    | The XML Working Group released XML in 1998.                                                        |
| **Format**               | JSON uses a maplike structure with key-value pairs.              | XML stores data in a tree structure with namespaces for different data categories.                 |
| **Syntax**               | The syntax of JSON is more compact and easier to read and write. | The syntax of XML substitutes some characters for entity references, making it more verbose.       |
| **Parsing**              | You can parse JSON with a standard JavaScript function.          | You need to parse XML with an XML parser.                                                          |
| **Schema documentation** | JSON is simple and more flexible.                                | XML is complex and less flexible.                                                                  |
| **Data types**           | JSON supports numbers, objects, strings, and Boolean arrays.     | XML supports all JSON data types and additional types like Boolean, dates, images, and namespaces. |
| **Ease of use**          | JSON has smaller file sizes and faster data transmission.        | XML tag structure is more complex to write and read and results in bulky files.                    |
| **Security**             | JSON is safer than XML.                                          | You should turn off DTD when working with XML to mitigate potential security risks.                |
