From the course: Learning Go

Parse JSON-formatted text - Go Tutorial

From the course: Learning Go

Parse JSON-formatted text

- [Instructor] When you make a request to a web service the data frequently comes back in JSON format. Go has a JSON package that lets you easily create and read JSON formatted text. My job now is to parse that text and turn it into structured data that I can use in my application. In this file that already has the code to download the content. I'll move the cursor down to the bottom of the file and I'll declare a new custom type. I'll name it tour and say that it's a struct. And I'll give it to fields, Name and Price. And they'll both be strings. Because these two fields have the same type. I can declare them on the same line and I've given them an initial uppercase character so that their public. Notice in my JSON content that the labels are lowercase. That's okay that JSON decoder knows how to match those values up. Next, I need a couple of new imports. I'll go to the top of my file and add these two lines in the import declaration encoding/json and strings. And then I'll create a new function that I'll call tours from JSON. I'll place this right here above this truck declaration I'll start with the func key word and give the function a name. And it will receive a parameter that I'll call content and its type will be a string. This will be the JSON formatted string that I got from the website. I'll return these values as structured data specifically a slice containing instances of that tour object. My first step within this function is to create the slice. I'll declare an object called tours and I'll use the make function and say that this is going to be a slice of tour objects and I'll set the initial size as zero and the initial capacity at 20. That's a guess. I'm not sure exactly how many tours I'm going to get but I know I'm going to get at least that many. Next I'll create a decoder object that I'll call decoder and I'll get that from json.new decoder. I need to pass in the value that I'm parsing and to do that I need to read it. I'll call the strings package and its new reader function and I'll pass into content value that was passed into this function. Now that the decoder has a link to that content. I'm going to check and make sure I don't have any errors. And I'll do that with this expression. I'll be calling a function called token from the decoder object. I don't care about the first value. So I'll name it with an underscore and I'm only looking at the potential error and I'll call decoder.token. Then I'll check the error and if it isn't nil I'll panic and get out of the application. Now I'm ready to transform my JSON formatted text into that slice of tour objects. I'll declare a variable that I'll name tour and I'll set its type as tour. Then I'll use a for-loop and this is going to be a wild style loop. I'm going to call a function called more from the decoder. This function will read the next available object from the JSON content. If it finds one it'll return true. And if it doesn't it'll return false. Within the function I'll call the decode function of the decoder object and it might pass back an error object. I'll pass in the memory address of that tour object I declared and that will fill in the data. Next I'll check my error. I'll just copy and paste this error checking code and put it right here. And then if I get to this point I can add that tour object to my tours slice. And I'll do that with tours equals append a parse and the tour's object and the tour object. And then at the end of the loop I'll return the tour's object from the function. Now I'll come back up here to the main function and I'm ready to call that new function. I'll create the object tours and assign it from tours from JSON and I'll pass into content. And now I can loop through the tours object. I'll do that with a for-loop. I don't care about the index and the for-loops. So I'll name that with the underscore. And I just want the tour from the tour slice. I assigned that with the range key word and the tour slice and then within the loop, I'll call fmt.print line and I'll pass into tour objects, name field. I no longer want to output my rogence on content. So I'll comment out this line and now I'll run the code. And the result is that I get back a listing of all the tour names. If you want to continue with the exercise you could convert that string formatted price to a floating number and format it. But the important lesson here is that Go has all the tools you need to work effectively with restful web services that return data in JSON format.

Contents