From the course: Learning the Go Standard Library
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Decoding JSON - Go Tutorial
From the course: Learning the Go Standard Library
Decoding JSON
- [Instructor] In a previous video, we saw how to encode JSON from data contained within a Go struct. And in this example we're going to see how to do the opposite. We're going to take some JSON and decode it into a Go struct type. So in our code editor, let's open up JSONDecode in chapter five, start. So here in my code, I have the struct defined. Which will hold some data about a person. And what I'm going to do is uncomment this code here in my decode example... function. And this defines some JSON text. And then what I'm going to do is create a variable to hold the decoded data. So to make sure that JSON is valid before I try to decode it. I'm going to use the valid function on the JSON package. So before I do that, let's go ahead and import the packages I'm going to need. So encoding JSON and FMT. So I'll check to see... I'll call JSON.valid on data. And if valid. Then we'll go ahead and decode it.…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.