- [Instructor] JSON is not the only data format in wide use,
and sometimes, when a program requests data,
it's only available in a format other than JSON.
Fortunately, converting is pretty straightforward
in many situations.
There are many data formats available, but aside from JSON,
the most common include XML, YAML and CSV.
XML stands for extensible markup language.
XML is related to HTML,
which is the markup language used to structure webpages.
XML can be customized in ways that HTML can't
and it's been widely used for data exchange
since before JSON existed.
YAML stands for yet another markup language.
You got to love it when the name of a language
to structure data can make you laugh.
YAML was created with the goal
of making structured data easy to write
and it has fewer rules than other data structures like XML.
CSV stands for comma separated values.
It's literally characters separated by commas.
CSV is often used for exchanging lists or tables.
When a data source doesn't offer JSON,
but a program expects it,
you need to convert it from one format to another.
To do this, you commonly use a library,
which is a set of computer code
that a program can call on to perform specific tasks.
Different programming languages use different libraries
for data conversion,
and often, many libraries are available to choose from.
For instance, in JavaScript,
you can use this XML to JSON utility.
Now this is an older utility,
and on the modern web,
a lot of frameworks are in use
that have this conversion capability built in.
But for learning purposes,
an older conversion tool like this is just the ticket.
In my script.js file for this chapter,
I have XML data stored in my raw data variable.
This would generally come in dynamically
by making an HTTP request,
but for our purposes,
I've included it to make the code less complex.
Instead of simply needing to parse the data
like I could do with JSON,
I first need to convert it.
I've saved the XML to JSON.js library file,
and I've referenced it within my webpage
so I can use its utilities.
This is a lot of data.
And then, eventually, we get to line two,
and here's where I need to add some things.
I first need to convert the XML string
to a JavaScript structure called a DOM node.
This is an intermediate step before I converted.
So I'm doing a var peopleDataNode,
and that's going to be
new DOMParser.parsefromstring rawData text/xml.
Now I've used the documentation for this tool
to understand how to write code that uses it.
The next thing I need to do to use that tool
is actually convert the XML in that DOM node to JSON.
So I'm going to create another variable called converted data,
and that's going to call XML to JSON
and pass it peopleDataNode,
and then a comma and two sets of double quotes.
Next, I can parse the JSON data for use in JavaScript.
So I'm going to create a const called convertedObject,
and that's going to be JSON.parse,
and I'm going to grab that converted data.
Now XML data uses an enclosing structure.
So I need to dig into that structure to get the data I want,
which is an array.
I've looked at the structure,
and I know how to reference that array,
so I'll add one more statement to reference that array
and save the reference with the variable name data.
So that'll be const data,
and that's converted object.data.product.
So I'll save those changes.
Then I'm going to open up products.html
and use my web server to open that up in the browser.
And now my product data is populated.
That data came in as XML,
but I used my library to convert it to JSON,
which my program requires
in order to display the data on the page.
Data conversion sometimes brings challenges.
If data picks up something as simple as an extra space
while being passed from one system to another
or it's formatted in a way
that the receiving system doesn't expect,
then the conversion process may require
some extra troubleshooting.
So it's always best to request data in the format you need
if that's available,
but it's not always possible to get data
in the format your program uses,
and converting from one format to another
is a standard part of working with data.