From the course: Learning Go (2021)
Unlock this course with a free trial
Join today to access over 24,400 courses taught by industry experts.
Parse JSON-formatted text - Go Tutorial
From the course: Learning Go (2021)
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…