You are currently viewing XML and JSON – The Basics

XML and JSON – The Basics

Serialized languages are used to communicate between APIs and endpoints. There are many serialized languages and all of them have their pros and their cons. In this post, I will focus on the two most common serialized languages: XML and JSON.

What is XML?

XML is short for Extensible Markup Language. XML uses tags to markup a document, allowing the browser to interpret and display information. In that respect, this is similar to HTML (Hypertext Markup Language). The difference with HTML language though is that XML is unlimited (extensible). It allows tags to define themselves and describe the content instead of only displaying a page’s content.

Like JSON, XML is not a programming language. A programming language consists of grammar rules and its vocabulary which is used to create computer programs. These programs instruct the computer to perform specific tasks. XML does not perform any computation or algorithms. It is usually stored in a simple text file and is processed by special software that is capable of interpreting XML. 

Using XML

XML is a markup language and is used to markup a document. Markup is information added to a document that enhances its meaning in certain ways. It identifies the parts and how they relate to each other. A markup language is a set of symbols that can be placed in the text of a document to specify and label the parts of that document. 

XML can be used for:

  • Working behind the scenes to simplify the creation of HTML documents for large websites.
  • Exchanging information between organizations and systems.
  • Offloading and reloading databases.
  • Storing and arranging the data, which can customize your data handling needs.
  • Merging with style sheets to create almost any desired output.
  • Expressing any type of data as an XML document.

How do you create XML?

Like HTML, XML can be created using any text editor. Below is an example of a very basic example:

XML Scripting

However, it is easier to create XML by using either an HTML editor with XML syntax highlighting. This can be done in Dreamweaver or in a text editor with syntax highlighting like Notepad++. 

XML language is unlimited regarding defining tags but there are still rules that have to be followed when writing XML code:

  • All XML opening tags must have a matching closing tag. In the case of our simple example, the opening tag was <recipe> to get the recipe. The closing tag to complete the records of the recipe is </recipe>.
  • As stated in the previous bullet, XML tags are case sensitive: your opening tag should exactly match the closing tag.
  • You must have a single root tag (element) with all other tags. A root tag is the first element to appear. This tag defines what kind of document the file will be. In our example, the root tag (element) is <recipe>. 
  • All tags that have an attribute must be in quotes. An attribute in our example is the 8 dL of flour for the recipe. The amount of 8 is an attribute which means you have to place it between tags: “8”.  
  • All tags must be properly nested. Nesting isolates the code it contains from code scoped outside the nested section. If you forget the </step> at the end of one of the lines in the instructions, the line is not correctly nested which means my command won’t be understood.

What is JSON?

JSON is short for JavaScript Object Notation and is a very popular format for storing data and exchanging data from one place to another. Most commonly between a server and a Web application. This is also the reason why JSON is the most used serialized language for APIs. JSON is based on the JavaScript programming language, but can also be used in most other languages with a special JSON parser. JSON is an alternative to XML for storing, associating, and accessing data. When saving JSON data, the file extension used is .json. Below is an example of a very simple JSON script:

Basic JSON script

Like XML, JSON is not a programming language. A programming language consists of grammar rules and its own vocabulary, which is used to create computer programs. These programs instruct the computer to perform specific tasks. JSON does not perform any computations or algorithms. It is usually stored in a simple text file and is processed by special software that is capable of interpreting JSON. 

JSON data types

JSON, formats data into JavaScript objects, and its value must be one of the following data types:

  • String
  • Number
  • Boolean (true or false)
  • Null
  • Object
  • Array

JSON Strings

String values in JSON are a set of characters, wrapped in double-quotes:

JSON Numbers

A number value in JSON must be an integer or a floating-point:

JSON Booleans

Boolean values are true or false in JSON:

JSON Null

Null values in JSON are empty words:

JSON Objects

JSON objects are wrapped in curly braces. Inside the object, you can list any number of key-value pairs, separated by commas:

JSON Arrays

JSON arrays are wrapped in square brackets. Inside an array, you can declare several objects, separated by commas:

The above JSON array has three objects and each object is a record of a person (with name, age, and length).

Nesting Objects and Arrays

JSON can store nested objects and arrays as values assigned to keys. Nesting isolates the code it contains from code scoped outside the nested section. If you forget the }, at the end of one of the lines, the line is not correctly nested which means my command won’t be understood. Nesting objects and arrays are very helpful for storing different sets of data in one single .json file: 

Nesting objects in JSON

Final thoughts

After researching the basics of XML and JSON, both serialized languages are more clear to me. Especially JSON was something I didn’t really understand and putting the basics in a simulator myself to see if these basics were correct in my example was a perfect way for me to understand a bit more about JSON and how this language is being used.

Feel free to contact me if you have any questions or if you have any additional advice/tips about this subject. If you want to keep in the loop if I upload a new post, don’t forget to subscribe to receive a notification by email. 

Gijs Groenland

I live in San Diego, USA together with my wife, son, and daughter. I work as Chief Financial and Information Officer (CFIO) at a mid-sized company.

Leave a Reply