JSON and XML

Let's start to learn important data formats, i.e., JSON and XML.

Handling XML and JSON Data

XML has been the go-to format and the only choice of programmers for open data interchange for a long time. Yet over recent years, JSON has also gained some popularity due to its lightweight nature and faster parsing. Regardless of these differences, they both have been adopted by all major programming languages of the world. In this chapter, we are going to dig deeper into how to read/parse, build XML and JSON formats, and will try to understand how serialization and deserialization work with these two formats.

Difference between JSON and XML

Generally speaking, JSON is a lightweight cousin of XML and there is no clear superiority of one format against the other. Our final choice should depend on what we need and our individual use case.

XML stands for Extensible Markup Language, which is an open-source language that is heavily used by programmers worldwide to store, persist, and transport structured data so that it can be read by other programs and softwares independent of the underlying operating system. This is because XML data can be stored in a text file and can be shared or exchanged on any operating system. It is extremely useful to store a small amount of data in a simple, human-/machine-readable structured format, which would otherwise require a SQL database.

XML Example:

<employees>
  <employee>
    <first>Prateek</first>
    <last>Singh</last>
  </employee>
  <employee>
    <first>Peter</first>
    <last>Parker</last>
  </employee>
</employees>

JSON stands for JavaScript Object Notation, which is a simple, text-oriented format to store data and exchange information between programs, browsers(client), and servers. It has slowly become one of the most popular and prefered data exchange formats compared to XML in various use cases and is now supported by most modern programming languages.

JSON Example:

{"employees":[
  { "first":"Prateek", "last":"Singh" },
  { "first":"Peter", "last":"Parker" },
]}
JSON XML
Stands for JavaScript Object Notation Stands for eXtensible Markup Language
Data oriented Document oriented
Simple to read and write Less simple than JSON
Less verbose, lightweight, and faster to parse Heavier than JSON
No support to display the content Capability to display data and stylesheet transformations (XSLT)
Supports array XML doesn’t support array

Reading XML from a file

PowerShell has access to the .Net class System.Xml.XmlDocument, which can be used to load an XML document and parse its content. All we have to do is instantiate this class using the New-Object cmdlet, then use the Load() method to load an XML file by passing a valid file path. Once the file is loaded, it will parse the XML document and convert it to PowerShell objects. In our example, we have the following XML sample saved as a test.xml file. This stores the employee data like ID, first, and last name into a structured format in child tags/nodes like <id>..</id>, <first>..</first> and under parent tags or nodes like <employee>..</employee>.

<data>  
    <employee>
        <id>101</id>
        <first>Prateek</first>
        <last>Singh</last>
    </employee>
    <employee>
        <id>102</id>
        <first>Bob</first>
        <last>Francis</last>
    </employee>
</data>
$xmldoc = New-Object System.Xml.XmlDocument
$xmldoc.Load('C:\Temp\test.xml')  #Path to XML file
$xmldoc.data.employee

Alternatively, we can also get the content of the XML file using the Get-Content cmdlet and then cast it to a [XML] PowerShell type accelerator, which is a shorthand for class System.Xml.XmlDocument.

Get hands-on with 1200+ tech skills courses.