(upbeat music)
- [Instructor] My first step is to stringify
the posts array.
So I have this array,
it contains objects and down here on line 17,
I'm going to write code that stringifies it.
So I'm going to create a variable first
and I'm going to make that postsJson,
and the value is going to be using JSON.stringify
and passing it hosts.
And so that is going to stringify my posts array.
Now, I also want to stringify the article object,
and again,
it's pretty much the same code.
I just need to use a different variable name.
So I'm going to use const articleJson,
and I'm going to use JSON.stringify,
and I'm going to pass it article.
So let me go ahead
and check just to make sure these worked,
I'm going to open up my HTML file for this blog.html,
going to open that with my web server.
So I've got the page loaded
and down in my console,
I can check postsJson
and I indeed have stringified version of that variable.
And I can check articleJson,
and I've got a stringified version of that object as well.
So that code worked.
And the second part here is in the issue.js file.
And I just want to do the reverse.
I have two variables here
and I want to destringify them.
I want to parse them so on line 4
I'm going to go ahead
and write code that parses the queue variable in line one.
And so I'm going to create a const called queueData.
And that's going to be JSON.parse,
and then I'm going to pass it the variable name queue.
And likewise below I have a variable on line six,
which is a JSON string containing an object.
You can see that curly braces,
the first character inside the quote.
And so I'm going to create another const called item data
on line nine,
and the value's going to be JSON.parse,
and I'm going to pass it item.
I'm going to save that.
And then back in my browser,
I want to check queueData.
And I have an actual array.
The browser recognizes this as a data structure,
so that's great.
It's no longer a JSON string
And I want to check item data.
That is an object that's recognized by the browser
as a data structure rather than JSON string.
So it's just using JSON.parse or JSON.stringify
to go back and forth between JSON
and data structures in JavaScript.
And again, if you're using a different programming language,
you're going to want to get familiar with the tools that do
the same thing in that programming language.
But generally these tools are available and you can easily
convert data to JSON and JSON, back to data.