So another thing that you use go for quite regularly is to read and write from Jason.
And if you're not familiar with Jason, don't worry about it.
There's an example right here in front of you.
Jason stands for Jason, as some people pronounce it.
It stands for JavaScript object notation.
And it's just a way of getting information from one place to another in a structured format and go makes
it really, really easy to work with Jason.
So typically, you would call some external service an API on some external service and you would receive
from your call a response that consists of a file like this, Jason.
So it's a structured format and this one actually starts and ends with square brackets.
So this is what you might think of as an array or a number one or more entries that all have a similar
format.
So in this case, we have two people.
We have one that's named Clark Kent.
His first name is Clark.
His last name is Kent.
He has black hair and he has a dog.
And the next one is Bruce Wayne.
And he has black hair, but he doesn't have a dog.
And I don't know if Bruce Wayne actually had a dog or not, but for the purposes of this exercise,
he does not.
So imagine, if you will, that you've called some external external service and you've received this
response and you need to get it into a format that you can use because this is just a string and this
is not something we can work with very easily.
So one of the things we can do is actually define a type.
So I'm going to define a type called person.
And this is just my own data structure and it is a struct and it has a few members and the members in
this type correspond to the entries in each one of these things in the adjacent file.
So I'll call my first one first name and it's a string and then I'm going to put this special tag after
it.
And this will tell go that when you receive or you're creating Jasen in this format, here's what you
use for the the name's Jason Colon, that in quotes first underscore name and this exactly matches what
is here.
So I'll do the same thing for last name.
And again, the entry in Jason would be last underscore name and hair color.
Jason Hare underscore Cullen and finally has dug, but this is not a string, this is a bull, Julian,
and this is going to be has underscored dog.
So now that I've defined this type, I can use that type to unmerciful.
Jason, this Jason file, which we're pretending we got from an external API into a struct.
So what we do, first of all, is we declare a variable and I'll call my variable unmuzzled and I'm
calling it that because the functions used in the Jason package in Gollan are called Mercial and unmerciful.
So this one is unmuzzled and it's going to be a type of slice person.
And the reason it's a slice is because this Jason, in the format that it's presented here has one or
more entries.
Here's one.
Here's two.
It might only have one.
It might have a thousand.
But I'm creating a slice because I know that what I'm getting from my Jason might have more than one
entry.
So how do I get it out of Jason into my variable that I've just created called on Marshall?
Well, it's quite easy.
Error is assigned the value of Jason, and that's the package in the standard library.
And I'm going to call the function on Marshall now.
And Marshall, when we point out it, you see that it takes two parameters.
One is a slice of bytes.
Well, this is a string, so we need to convert that to a slice of bytes.
But that easy, that's easy.
And the second is an interface that you're going to put the contents of this slice of bytes into, and
that will be our variable on martialled.
So how do I convert from a string to a slice of bytes?
Well, it's quite easy.
Slice byte.
And then in parentheses, the data that I want to convert to a slice of bytes.
And in this case it's the variable called my JSON.
And where do I want to put this?
I want to put it into a reference to a commercial.
Now we check for our error.
If error is not equal to nil, then I shall just say log print line error, martialing Jason and then
I'll print the error.
All right.
So once I have this, what can I do with it?
Well, let's print it out.
Log print F and I'm going to use the print F because I want to substitute something for a placeholder
and I'll just put on martialled.
Oops, I got to put that in quotation marks on martialled colon and then present the subject, putting
an interface in there and what I want to put in there is my variable on martialed.
All right.
So let's see if we got this right.
Let's run this go run may not go.
And we have this value given back.
And Marshall is Clark Kent Black.
True.
Bruce Wayne Black Fault's, which exactly corresponds to the entry in Jason in the Jason file.
But now it's been converted to obstruct.
So that's pretty easy.
So we can read Jason into a struct.
How do we write Jason from a struct.
Well that's pretty easy to write, Jason.
From a struct.
Well, first of all, let's create a struct.
So I will call the struct my slice and it's going to be a slice.
Sorry, Daria, that again ver my slice is going to be a slice of person.
And now let's put some values in there.
So I'll say ver mercial, I'll call it meone just for Mercial me one and it's going to be a person and
let's put some values in there.
And one dot first name is equal to I don't know, we need another superhero Wally and one dot last name
equals W.
And one good hair color equals red and one has Doug equals false, I'm not sure if the Flash has a dog
or not, but again, this is just the purpose of this exercise is just to show how things work.
So my superhero knowledge may be somewhat limited and I apologize for that.
Now I'm going to append this and one variable to this slice.
So I'll put an entry into this slice and that's pretty easy.
My slice equals append and I'm appending to my slice and I'm putting in the value add one.
Now let's copy this whole thing and just change the variable names and the values.
So we'll have to represent our slice of our M2 and I'll change these all to M2.
And I'll change this to Diana.
Prince.
Black and she does not have a dog either, and now I'll put em to into this slice.
So now I've created a slice that consists of two entries it has and one which is not called in, one
in the slice, but it has an entry with Wally West and an entry with Diana Prince.
So now I want to convert this into Jason and I'll create a variable called Jason and potentially an
error.
And that is a sign the value of and we call the Jason package again.
And this time we want to Marshall.
Now I'm going to use Marshall indent just because it makes it more readable.
All that does is format our Jason nicely.
If I just use Marshall, it wouldn't have any nice tabs or line breaks.
It would make it really as small as it possibly could.
So in production, when you're sending Jason, say, to a remote request, say you build an API that
responds with Jason, you probably wouldn't use Marshall in debt.
But for development purposes, when I want to look at the content that I'm producing, I always use
Marshall in debt.
So what are we martialing into Jason?
We're marshaling my slice and it has no prefix.
You can put a prefix in there if you want to, but you rarely do, or at least I rarely do.
You might find situations where you have to.
And how much do you want to win?
It's identity, a few spaces.
So we've called that we should check for our error.
If error is not equal to nil, then I will log print line error martialing.
And I'll just put the air in there, otherwise I want to print this out.
Now, what does Jason Marshall intent give us?
It hands back a slice of bytes and potentially an error so I could just write the slice of bytes, but
that would not be very useful.
You wouldn't get much useful information from that.
So instead, let's just use our format, package format or print F or format print, and we'll convert
our slice of bytes into a string by saying String and new Jason.
All right, let's try this out and see how it works.
So I'll clear the screen.
And I'll make this a little bit bigger so I can see what's going on.
Let's run this go, run, mean, go.
So first of all, I get the content from the first part of our program where it actually creates a struct
from Jason, there it is.
And then it outputs my JSON that I constructed as a slice of struct.
It outputs it as Jason, and that's all there is to it.
Now, this presupposes that, you know, the format of the JSON that you're receiving, if you're receiving
JSON, if you don't know the format, there's a way to handle that.
But I'm not going to cover that in this lecture.
If we run into that situation later in the course, we will cover it.
But in the most cases, it's it's always best if you know the structure of the duration you're going
to be receiving, just write a struct.
The conforms to that structure and it will make your life ever so much easier.
All right.
That's enough for this time around.
Let's move on.