Golang Json Conversions - Built-in Json Library in Go

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what's up ninjas and welcome back to the goliath dojo where we talk about everything related to mastering the goal for women language and eventually becoming golang ninjas together if that sounds interesting to you at all make sure to subscribe to the channel i don't know why you wouldn't since you've already clicked on this video so today we will talk about something slightly more interesting and that is jason and how girl handles a json and more specifically how to convert a json object into a go object and vice versa without further ado let's get started to get us started we have created a struct of a ninja here that has a name weapon and a level so if you don't know about instruct in a go then make sure to check out my last video where i talked about struct in and go now the first thing that we want to do since our first objective is to convert a string a json string into a go object so what we need at first is going to be a string that we're going to convert into the go object from we're going to call it as a thumb and then we're going to copy and paste the json string it's not the easiest thing to write a json string now let's just put this string out as you can see we are getting this adjacent string here it has a attribute name name the name is going to be wallace wallace has a weapon and that weapon is a ninja star and was as a level and he's at a level one the second thing we need to do is we need to create the actual go object of type ninja then we will call the json library and unmarshall byte we are going to convert the string into the byte slice and then we are going to pass in the address of the wall as a variable also if you don't know anything about pointers and addresses make sure to check out my last video where i talked about pointers and addresses in go so we also need to make sure that the error that we are receiving is not null if it's not null then we need to print out sad boy here because it's not good otherwise we can just print out wallets again here assuming that there's a martial function from the json library will and marshall this json string and start occupying all of the fields in this wallet ninja object here which is a struct that has a name weapon level just like we have in this adjacent string here i said let's go ahead and print this out as you can see we are indeed printing out the full content of the wallace object here and then the wall's object is indeed being occupied unlike it before well if we also want to make sure that is indeed the case we can print out wallace here as well as you can see wall is it doesn't have anything to start with the name is going to be empty weapon string is going to be empty and the level is going to be zero because the zero value is for integers is a zero if you don't know what the zero values for integers and all that stuff make sure to check out my other video where i talked about numbers and math in the goal so this is how you would unmarshal a json string into a go object now know that the only reason the programmer knows to map which field in the json string or json object into which field in the wireless object here is because they have the exact same names name weapon and a level however if that were not to be the case then we need to actually do something else so if we try to change this to full name here change the json object full name then we can see that these two fields don't have the same name so if we run this program again we should be able to see that yeah something is off here because the program doesn't know how to convert the full name doesn't know that the full name field is the same as the name field in order to fix that we need to explicitly tell the program that hey we are going to use this field for json the conversion and the field that we are going to convert the json objects from into this name field is going to be the full name field so if we go ahead and run this program again we can see that wall is the name or the full name is going to show up again of course if you are a paranoid person you can copy and paste this like such and then actually explicitly tell the program that it is a weapon fill in the json object and this one is the level field in the digits and objects so in the case that we are going to change this field name at a later time the program is not going to break so if we run this it's not going to change anything because again these two already have the same name okay now that we have our wallets and ninja object constructed from a json string let's convert it the other way around we are going to declare a string two instead of string form and we're going to handle the errors in just a little bit but we'll call the json library and this time we are marshaling there's a go object into a json string so this time is slightly simpler we just need to pass in the wallet ninja object and then check for the error we call it uh sad point again and we should be able to see that if we print this out that the string is going to locate something like this go ahead and run the program as you can see we are seeing something that is doesn't really make any sense and if you remember we actually converted the json string object into a byte slice and what we get in return is actually the by slice also so if we try to print out the type of s2 we should be able to see that it is a byte slice if you go to bite here it is the underlying type of advice is unsigned into 8. so this is why we would want to actually put a new line string little here we want to convert it into a string by saying that this is a string that we are trying to print out here and if we want run this program again as you can see we are getting the exact same strings just like the string that we are converting it into a go object from now the structure that we use is a pretty simple struct pretty basic data types string string and end now let's try to make the types inside of the ninja strokes slightly more complicated and see if a girl can handle it perfectly fine so if we have another type it is going to call the weapon struct and the weapon stroke is all going to have a name for the weapon and this weapon is going to have a level just like the ninja and we are going to give it a weapon name we're going to tell the program that hey we're trying to convert it into and from json objects here it's a weapon name we're going to give it a weapon level here we don't have to do that but this will be a slightly more paranoid version but what we want to do now is well let's try to run this program again first after changing it to a weapon and run this program and see what we get now we can see that we are getting a full name wallace weapon and then we have we don't have anything for the weapon name we don't have anything for the weapon level and the ninja level is one stone so what we're gonna do is we want to actually make sure that this is a object that we are constructing this object is going to have a weapon name the weaponing is going to be a ninja star and then we're going to have a weapon level the weapon level is also going to be scroll down a little bit here going to be one so this one just should give us should occupy the weapon which in turn assign some value into the weapon name and the weapon level here so if we run this program again for some reason it's not doing the job here so let's try to debug this and see what is wrong what is going on here as you can see we are seeing a sad boy here and let's go ahead and try to print out this error maybe you can learn something from me trying to debug the error here so we see that we have an invalid character after the object a key so what is the invalid character as you can see we don't have a closing double quotes here we're going to just put this as a one here like that because it is a number so we try to run this again now we can see that it is giving us the full name of the ninja which is wallace and we are converting the original json string that contains there is a weapon object here into a go object and then back to a json string here and correctly so these are the basics of adjacent conversions in nikhil keep in mind that all of the tutorials that i've been producing including this adjacent tutorial are based on the book the go programming language and for the next few dozens of videos i'm gonna go through some of the most popular technical books just like the book go programming language on the channel and actually make video content out of these books so if you want to learn the go program language but you hate reading technical books and this is the channel for you because i'll be doing all that work for you so make sure to join the go like dojo by subscribing and let's get started with our journey in mastering the gopro global language and becoming a golan ninjas together without let's shout again in our next video [Music] you
Info
Channel: Golang Dojo
Views: 1,864
Rating: undefined out of 5
Keywords: golang, golang 2021, learn golang, go lang, golang in 2021, go language, golang tutorial, go tutorial, go programming language, golang tutorial for beginners, golang crash course, golang for beginners, golang json, json in go programming language, golang built in json library, Golang Json Conversions - Built-in Json Library in Go, golang json conversions, golang json file, basic json in go, json in golang, json encoding tutorial in golang, json encoding in go, json in go
Id: 96TCeWJ7kHg
Channel Id: undefined
Length: 10min 39sec (639 seconds)
Published: Sat Jun 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.