Whether you are building a thin client (web application) or a thick client (client-server application), at some point you are probably making requests to a web server and need a good data format for responses. Today, three main data formats are used to transmit data from a web server to a client: CSV, XML, and JSON. To develop an application with a strong architecture, it is a good idea to understand the differences between each format and to know when to use them. The purpose of this post is to define each data format, expose the pros and cons of each, and find out which situations work best with each format.

CSV

CSV stands for “Comma Separated Values”. As the name implies, this data format is basically a comma-separated list of items. Let’s say your response is to submit a list of people from a particular family. The format would look like this:

Eric, Andrea, Kusco

Advantages: this format is the most compact of the three. Generally speaking, CSV formats are about half the size of XML and JSON formats. This is the main advantage of CSV because it can help reduce bandwidth

Cons: This format is the least versatile of the three formats. This is because a homemade parser is required to convert the CSV data to a native data structure. As a result, if the data structure changes, there is an associated overhead of having to change or even redesign your parsers. Also, since the program that creates the CSV and the program that parses the CSV reside on different machines (remember that we are passing data from one machine to another), both programs must be updated simultaneously to prevent the receiving program from crashing. Otherwise, a break is required to update both programs individually to avoid incompatibility issues.

Finally, CSV doesn’t really support data hierarchies. What if you wanted to return the attributes of each person in each family? Then you would have to design a complex parser that knows which parts of the CSV refer to elements of a family and which parts refer to elements of each person. One way to solve this problem is to use another delimiter such as “;” to separate the attribute of each person:

Eric; man; 26, Andrea; woman; 26, Kusco; man; 8

However, the problem with creating custom formats is that there is an overhead in maintaining an even more complex parser.

XML

XML stands for “Extensible Markup Language”. XML was designed in 1996 and officially became a W3C standard in 1998. It was created to better represent data formats with a hierarchical structure. The format looks like this:

<person> <name></p> <p>Eric<br /> </name><br /> <age></p> <p>26<br /> </age> </person> <person> <name></p> <p>Andrea<br /> </name><br /> <age></p> <p>26<br /> </age> </person> <person> <name></p> <p>Kusco<br /> </name><br /> <age></p> <p>8<br /> </age> </person>

Advantages: This data format is fully compatible with hierarchical data structures and is very appropriate when receiving complex data as a response. It is also very human readable. Most browsers have built-in XML readers that allow you to inspect XML files. Since XML was the first standard hierarchical data format, most APIs have built-in functions to automatically convert XML data streams to native data structures such as objects.

Cons: This data format is roughly three times larger than CSV. This is because each data item has an opening and closing parameter tag associated with it.

JSON

JSON stands for (Javascript Object Notation). It was invented in 2001 and popularized by Yahoo and Google in 2005 and 2006. It was created as an alternative to XML. However, like XML, it represents hierarchical data with the use of commas, braces, and square brackets. An example of JSON looks like this:

{“name”: “Eric”, “age”: “26”},

{“name”: “Andrea”, “age”: “26”},

{“name”: “Kusco”, “age”: “8”}

Advantages: This data format supports hierarchical data and is smaller than XML. As the name implies, it was also created to more easily parse data in native Javascript objects, which makes it very useful for web applications. JSON is the best of both worlds with regards to CSV and XML. It is simple and compact like CSV, but it supports hierarchical data like XML. Unlike XML, JSON formats are only twice as large as CSV formats.

Cons: This data format has slightly less compatibility than XML. Since JSON is relatively newer than XML, there are fewer APIs to automatically convert JSON to native data structures. However, this is changing rapidly because newer APIs and plugins support XML and JSON.

conclusion

As a general rule, JSON is the best data exchange format to date. It is lightweight, compact, and versatile. CSV should only be used if you are sending large amounts of data and if bandwidth is an issue. Today, XML should not be used as a data interchange format because it is more suitable for document markup.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *