How do I compare two JSON requests

JSON, XML and YAML in comparison


For Web APIs and configurations, XML is increasingly being replaced by simpler formats such as JSON and YAML. For example, Twitter has discontinued its support for XML in its streaming APIs. This article compares JSON, YAML and XML and shows the strengths of the formats for certain areas of application.

JSON

JavaScript Object Notation is a format for serializing objects. It was specified by Douglas Crockford. The syntax of JavaScript was used for the notation. Thus, a JSON document is also an executable JavaScript.

The usual separation of data (e.g. XML documents) and code (e.g. Java) is thus avoided. Even the Lisp language from the pioneering days of EDP made no difference between code and data. The use of JSON for the asynchronous transfer of data between servers and websites is particularly popular. JSON has now almost replaced XML as AJAX format.

Thanks to the simple notation, JSON documents are easy to understand even for people. Listing 1 shows an example of an order. The notation for a JavaScript object was used here. The object has the four properties id, date, customer and items. Id and customer are of type Number. The property date is of the type String. There is no special data type for a date in JavaScript. Items contains a list of two objects.

{"id": 731, "date": "16th of May 2011", "customer": 17, "items": [{"quantity": 5, "description": "Oolong", "price": 5.98, "in-stock": true}, {"quantity": 2, "description": "Assam", "price": 2.95, "in-stock": false}]}
Listing 1: Example of an order in JSON

You will find the corresponding YAML and XML documents for the same order in the following sections.

The strengths of JSON lie in a clear notation, low overhead and good support from a large number of programming languages.

YAML

YAML is the short form of "YAML Ain't Markup Language". YAML became known as a format for configuring Ruby on Rails. In contrast to XML, the language for data exchange is easy to read and edit. This was achieved by eliminating brackets, quotation marks and tags. YAML uses indentations to represent hierarchical structures. This makes the order in Listing 2 even more compact and clear than the order in JSON format.

--- id: 731 date: 16th of May 2011 customer: 17 items: - quantity: 5 description: Oolong price: 5.98 in-stock: true - quantity: 2 description: Assam price: 2.95 in-stock: false
Listing 2: Example of an order in YAML

In the example we find a list of positions (items). As in word processing, a list is created by placing a bullet in front of it.

Since YAML is a superset of JSON, brackets can optionally be used to achieve JSON compatibility.

YAML is line-oriented and can therefore also be processed by tools such as awk or grep.

In addition, YAML offers some features that cannot be found in JSON and XML. For example, in Listing 3, a Map cities1 defined with capitals. The map cities2 extends the first map to Rome and corrects the entry for Germany.

- cities1: & capitals Germany: Bonn France: Paris - cities2: <<: * capitals Germany: Berlin Italy: Rome
Listing 3: Avoidance of redundancy with relational data

YAML also supports data types and type casting.

XML

Since the release of the XML 1.0 specification, XML has spread widely. There are thousands of markup languages ​​based on XML. XML has penetrated almost all areas of application. However, there is one area that continues to resist the dominance of XML. Although XML was created to solve problems in web design, there is a strong dislike for it. For example, Firefox and IE namespace declarations in XML documents simply filter out rather than display them.

Listing 4 shows the order already known from above in XML format.

<order> <id>731</id> <date>16th of May 2011</date> <customer>17</customer> <items> <item> <quantity>5</quantity> <description>Oolong</description> <price>5.98</price> <in-stock>true</in-stock> </item> <item> <quantity>2</quantity> <description>Assam</description> <price>2.95</price> <in-stock>false</in-stock> </item> </items> </order>
Listing 4: Example of an order in XML

The order in XML format has a higher overhead and is more confusing than the JSON or YAML document. If we left our comparison at that, XML would be the clear loser. In contrast to JSON and YAML, XML is a markup language. XML can therefore be used for more than just serializing objects.

In Listing 5, the same order was provided with additional XML features. Elements from other XML languages ​​are now used in the document. The id Element points to a REST resource via XLink, which can be used to request representations of the order. The address has been included and the description of the second position now contains HTML markup.

The short example also shows how horrible XML documents can be. If you were to add namespaces to JSON or YAML, these would probably become just as confusing.

<order xmlns="http://predic8.com/order/V1/" xmlns:html="http://www.w3.org/1999/xhtml"> <id xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="http://server/order/731">731</id> <date>16th of May 2011</date> <customer>17</customer> <basic:address xmlns:basic="http://basic-address/de/"> <basic:city>Bonn</basic:city> <basic:street>Moltkestr. 40</basic:street> <basic:zip>53173</basic:zip> </basic:address> <items> <item> <quantity>5</quantity> <description>Oolong</description> <price>5.98</price> <in-stock>true</in-stock> </item> <item> <quantity>2</quantity> <description><html:b>Assam</html:b> Black Tee</description> <price>2.95</price> <in-stock>false</in-stock> </item> </items> </order>
Listing 5: Example of an order in XML with embedded XLink and HTML

The structure of XML documents can be described with XML schema. Listing 6 shows an excerpt from a schema with the definition of a type for an item in an order.

<complexType name="itemType"> <sequence> <element name="quantity" type="int" /> <element name="description"> <complexType mixed="true"> <any namespace="##any"/> </complexType> </element> <element name="price" type="float" /> <element name="in-stock" type="boolean" /> </sequence> </complexType>
Listing 6: The scheme of an item

The format description with XML schema offers numerous possibilities:

  • With the help of the schema, XML editors can recognize and display errors or offer auto-completion
  • Code for the serialization of XML documents can be generated from XML schema
  • The scheme can serve as documentation
  • XML documents can be validated against the schema.
In addition to XML schema, there are a large number of standards and tools in the XML world for generating and processing XML.

The following list contains just a few examples:

  • Queries with XPath and XQuery
  • Transformation with XSLT
  • Storage in an XML database
  • Routing via an ESB

Cosmopolitanism

By using namespaces, elements and attributes in XML can be uniquely named worldwide. An XML document can be interpreted separately from an application. With JSON and YAML documents, the content only makes sense through the application that reads the document.

The namespaces give XML documents a context in which they can be interpreted. XML advocates therefore accuse other formats of provinciality.

Web support

With XML, the REST idea that everything is a resource can be easily implemented.

A reference to a resource can be added to any information via an XLink attribute or an enclosing element. This is not possible with JSON or YAML, although they pretend to be “web” languages.

Conclusion

JSON has challenged XML's sole rule and shown that it can be used for special tasks such as B. the serialization of objects is a leaner alternative. JSON and YAML also show that readability can be improved. XML still scores with its versatility, tool support and the large number of standards that are based on XML or can be combined with XML.

swell

Introduction to JSON
YAML Ain’t Markup Language (YAML ™) Version 1.2
Extensible Markup Language (XML) 1.0 (Fifth Edition)