Generating JSON From Golang

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] foreign [Music] Tech my name is Gerald and in this video I'm going to show you guys how to work with Json in go very important stuff for every go Lane developer to be specific I'm going to be showing you guys how to generate Json data from your code this is the first of a two-part series and the second part is going to focus on how to ingest Json data so make sure you subscribe so you can be notified when that second video was posted let's take a look at the agenda I'll start off with an overview of Json and then I'll go through some Json examples which are ultimately what we're going to be creating lastly I'll hop into the code and I'll show you a few different ways you can generate Json from go I also want to say I'll be resuming the use of pop-up tips like this one so nostalgic but I received some much appreciated feedback that the pop-up tips were useful in my previous videos if this is your first time on this channel I use those pop-up tips as a way to provide additional Insight around a topic that I'm discussing you can think of it as tips to help you to become a more complete developer now let's get into Json first thing to know about Json is that it stands for JavaScript object notation and it's essentially a format that's used to represent or store data so just like a struct is used in golang or a class is used in object-oriented language Json is similar to that in the sense that it can it can hold data but the good thing about Json is that you can use it outside of JavaScript even though it's got JavaScript in the name you don't need to know JavaScript to work with it and in fact a lot of the time you work with it you won't even be touching JavaScript I'll hop over to that last bullet point on the left in a second but I want to go through the common use cases one of the most common use cases for Json is when you're working with apis so if you're a back-end developer what you're essentially doing is building the back end for some service that's going to return data to the front end the most common format to return this data is Js side and so regardless of whether you're a front-end or back-end developer Json and understanding it is pretty much you pretty much have to do that it'd be tough to find a job if you don't know how to use Json and by the way there'll be a future video on apis if you want to learn more about that it's also used in configuration files so if you want to store data about some program or for example let's say you're you're designing a video game and the character the user is saving the game and you want to save information about uh the game itself you know what level they're on what character they're playing with their health all that information you might store that in some file and store it in Json format Json is the primary data format used in no SQL databases but it's also used in SQL databases you can actually assign that data type to a column just like you can assign a you know character or a numeric type and then it's also used in schema descriptions so when you're going through the planning phase of an API or you're conceptualizing some object that needs to be used across your domain a lot of times that'll be represented in Json format going back to that last bullet point on the left I'm sure there are plenty of alternatives to Json but a couple of the popular ones are XML or yaml personally I'm not a big fan of XML but each of those do have their individual use cases as well that leads us into the question of why do we use Json versus some of the other data formats well for one it's easier to read versus formats especially like XML it's got a small footprint it's relatively lightweight so it doesn't doesn't consume a lot of memory doesn't take up a lot of bandwidth when you're sending it across a network also integrates well with JavaScript which is the predominant front-end language so JavaScript is considered to be a superset of Json so it's syntactically similar when you talk about the the objects in JavaScript and the actual Json data structure itself and it's kind of nice that the front-end language inherently knows how to parse and ingest that data it also works well with other languages which is what we'll see in this tutorial and then there's plenty of support and tooling around it lots of libraries plugins command line tools documentation Etc now just like golang has a set of basic data types Json has that as well and that's what you see here a couple things worth noting we have the number data type at the top it's important to know that Json makes no distinction between an integer and a fraction or a float all of those numeric types fall under the number data type we've got null which represents an empty value and then we've got an array and an object and even though those are data types those are also considered to be Json data structures which means that they essentially store other fields or values so most of the time when you're sending Json or storing Json it's going to be encapsulated inside of an array or an object now quick break for the first of hopefully mini sessions which I'm appropriately naming as an ET extra these are sessions dedicated toward helping you understand how you would actually apply this information in a real engineering row which I know many of you are hoping to obtain there are so many programming channels out there that are showing people how to do things in a way that really doesn't align with how you would use them in a real world scenario and we're going to fix that now if all you care about is the coding person go ahead and forward to that section but what I want to explain is why am I selling you the Json schema first and then hopping into the golang code second well the answer is that's how you would do it in a real world scenario typically if you're developing an API for some front-end service you're going to work with your front-end counterparts to understand what the front end expects and they want to understand what the back end is going to send once that's agreed upon you're going to document that in the form of what's known as an API documentation or specification and then you'll go and write your code accordingly even if you're writing a service that's going to make a requests to some other web API for example the Google Maps API you're first going to go look at their API documentation and then go and write your code very seldom are you going to just write your code and be happy with whatever Json it spits out and the reason that's important is because if you've never done that before if you've never written your code to align with some pre-existing specification the first time you look at it you may find that it's more challenging than you initially thought and that is an ET extra now let's take a look at our Json examples as I mentioned I found these on their web and this outer object has a five total field so we've got name skew price shipped to and build two uh this is a string string number type and then for sip2 and build to those fields have objects assigned to their values and so if I'm thinking about the go laying equivalent of this entire Json object here I'm thinking about you know creating some struct called order and then for these fields it's probably appropriate for me to create some other struct type and and based off of the names and values probably a struct type called address that has these values and then in my order struct assign uh or set these to be of type address right let's take a look at orders.json so this one similar except for the fact that uh it's within an array so we've got an order here an order here uh similar fields to the last one but it is within a rate so this is an array of order so we need to figure out how we can uh represent that in golang so let's take a look at how we can do this first one order.json as always start with my package main I already know the packages I want to import the one of interest is going to be the encoding slash Json packets that's the one that's going to do the heavy lifting and contains the functions we're going to call to convert our code over to Json I also need the following packages uh now I can create my address struct type so type address which is a type struct a couple things to note whenever you're creating a go Lane struck that you intend to be marshaled over the Json the fields have to be uh imported field so I'm sorry exported Fields right they must be accessible by other packages in this case the encoding slash Json package needs to be able to access those fields right and so I'm going to make sure I use a capital letter for the field names another thing to note here is that by default the field name that you give it in golang is the field name that will appear in the Json object but here we have a difference you got a capital N here lowercase n there uh and I but I want this result and so the way to do that is to use something called Json tags and the Syntax for that is to use the back ticks followed by this Json keyword a colon and then within parentheses I'm going to put the actual field name that I want to appear in my Json object right and this ensures that regardless of the golang field name this is the thing that you actually see in the output and I'm going to do that for each of these fields it's really just good practice to always add Json tags even if the field names are exactly what you want them to be I also need an orders struct all right so now my funk main I'm going to create an instance of this order I'll call it John uh and I'm just going to give it the values that I know that I want to appear in my Json output just sip two so I've got my instance there and now the question is you know what are the various ways that we can call functions from that Json package to convert this over rather than kind of write one and explain one I'm going to write them all to start off and then explain each of them okay now in order to make it more clear so you can see what I'm doing with each function I've encapsulated the logic into my own functions right and try to name them appropriately the first one I want to look at is this Marshall order to Json the most important function here is this json.marsal as I mentioned we're calling that from the Json package and if I just look at the function here in the signature we can see that this takes any which is basically just an alias for an empty interface right it's just a type that they've defined in the in the Json packets which is a type empty interface and it returns a slice of byte and an error that slice of byte is our output that's the Json result and then as oft as usual with a lot of you know go laying functions it returns an error that we want to make sure we're going to check but the key thing here is that we can pass in an empty interface right any interface type and we can expect to get back uh our result but ideally really what we want to make sure we pass in is a struct all right and so here I've defined my instance and so I'm just going to make a call to my own function here and pass in John I'm going to go ahead and print that out and this is the result here we can see that we get a Json object our field our field names are correct right they're lowercase which is what we want it uh and so that looks good there that's just kind of a basic example of how to call json.mercel now one thing to note here is that all of this stuff is on one line if I was going to save this output to a file it would look just like this in fact the only reason it looks like it's on two lines is because my screen isn't wide enough but it really is all on one line but what if I want my output to look like this well in that case I put I've created a function here called Marshall order to pretty Json and the difference here is that instead of calling Json dot Marshall I'm gonna call it Json dot Marshall and dent and if I look at the signature for this function again we take in any so that's going to be the you know the data we want to convert over to Json then we've got a prefix and we've got indent and this is uh just like Marshall but applies indent to format the output each Json element in the output would begin on a new line beginning with prefix followed by one or more copies of ending according to the indentation nesting so what it's saying here is that it will prefix every line with this and then it will apply these indents based off of the nesting of the fields and that's kind of what we want here right like just here these indents get applied based off of the nesting so again I'm going to call this function and pass in my instance all right and then run that again all right and we see now we've got our original input here Marshall the Json and then the second one which is Marshall order the pretty Json we've got that looking just like it does over here on this side now I've got another example down here which doesn't use the Json dot Marshall function instead it uses this json.new encoder function right what's that about so here uh this function if I look at the look at the signature here it takes in an IL writer right and that's key because if you look down here I've labeled kind of the output here but I don't have anything that says like you know print the result there's no print line statement like here but this Json that new encoder when I create this instance of an encoder I tell it exactly where I want my output to be sent and so I'm using this OS dot standard output which is just a variable in the OS packets and this standard output essentially represents my terminal right so I create an incr an encoder instance of that and then I call the set indent function which this is optional this is essentially the same as Marshall indent right so we've got prefix and end it and then last thing we want to do is call encode the encode function on this encoder and then I'm going to give it my input and the input and the signature for this is an empty interface just like the json.marsal functions uh and it just returns an error because it's already going to send the output to this place right here right and so I'm going to go ahead and call that function foreign so now the result is here right and this looks the exact same as the calling the Marshall indent function all right so now I've shown you a few different ways you can create a Json object from a struct in go but you can also create Json from a map of string to empty interface that's because the layout of a map closely resembles the layout of a Json object now show you what I mean here instead of passing in my struct what I'm going to just do is create a map of string to empty interface from scratch here pass that into the json.mercial function and we'll see that the result looks very similar to uh to when we pass in our struct all right so this particular function I'll call Marshall map to Json right and then order is going to be a map of string to empty interface right and in this map the field names or the keys are going to be the field names of the Json object and the values are going to be the values and then for the ship 2 that again is going to be a map of strain to empty interface and then the same thing for the BL2 all right and then we want this to be printed out nicely uh and so I'm going to call it json.mersal indent all right and we see here that uh Marshall map to pretty Json contains all of the same Fields but it's ordered a little different and that's because uh the the order and maps are not guarantee and in fact it looks like it put it in alphabetical order which is fine uh actually the the order of the fields in Json doesn't really matter just like the order of fields and structs it don't really matter um but yeah it just comes out a little different but typically you would not do this you would not uh create Json from a map of strain to empty interface uh typically you're just going to be using structs okay so now how do I create this orders.json how do I create an array of order well that's actually pretty simple the go laying equivalent of an array is a slice so I'm going to comment out that code right there and I'm going to create another instance of uh this order so this one I'll call Alice and then at the end here what I'm going to do is stick both of uh these instances of order John and Alice into a slice of order right and then I'm going to make a call to my function that I created down below Marshall order to Json all right and in fact I think I'm going to say Marshall order to uh pretty pretty Json all right and now here we can see we've got those orders nicely uh within an array encapsulated within an array there a couple last things I want to show before I end this video at the beginning of this video I showed a few keyword keywords uh one of those keywords was omit empty which is kind of an important one a mid empty essentially will prevent any value from being converted or marshaled over to Json if that value is the zero default value for the field so for example if I change this to zero say zero dot zero which is default the default zero value of a float 64 and if I run this all right look at the output here you see that we see that value right here right price is zero but if it's a field that was never actually set in my go code this could be misleading it could be that that value was was never actually set and it just defaulted to zero but then in the output it actually looks like the the real value should be zero right and so in order to prevent that what I can do is I can go to that field which is here and then I'm going to add the keyword omit empty and what that does is I mentioned is preventative checks to see hey does this field equal it's default zero value if it does and I'm sorry this is supposed to be a comma there so it comes a comma omit empty right after uh the Json tag and then I'm going to run this again now and now we can see that field has been omitted right it's not there anymore because it was the zero default value and then if I want to just completely omit a field regardless of the value I can completely uh change the Json tag so let's say that I want to Omit this SKU number I'm just going to put a dash there instead and now again when I run this code now we see that the skew number has been omitted and I think this video has probably gotten long enough so rather than code out uh the the go Lane to create this complex.json I'll issue a little challenge what I'll do is share this file and I encourage you to go through it then see if you can write up the goaling code for it right and then I'll also post a solution so that you can compare what you got to the solution that I've posted and of course feel free to reach out ask me any questions that you might have but hopefully you guys have found this video to be informative certainly try to make it uh as helpful as I possibly could as as always I appreciate you guys taking the time to watch this video please do subscribe like let me know if you have any questions feel free to comment and I will see you on the follow-up video to this which is uh how to ingest Json in your golang code okay
Info
Channel: Esoteric Tech
Views: 760
Rating: undefined out of 5
Keywords: go json tutorial, golang tutorial, golang, programming, tutorials, coding tutorials, json unmarshalling go, go marshalling json go, go encoding/json tutorial, go working with json, learn golang, go programming language
Id: Hph6PQQY6Ow
Channel Id: undefined
Length: 23min 37sec (1417 seconds)
Published: Wed Nov 09 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.