How to Parse JSON in Golang?

10 Jul, 2020
Golang provides multiple APIs to work with JSON including to and from built-in and custom data types using the encoding/json package. To parse JSON, we use the Unmarshal() function in package encoding/json to unpack or decode the data from JSON to a struct.
Syntax: func Unmarshal(data []byte, v interface{}) error

Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
Note: If v is nil or not a pointer, Unmarshal returns an InvalidUnmarshalError.
Example 1:
// Golang program to illustrate the// concept of parsing json to structpackage main  import (    "encoding/json"    "fmt")  // declaring a structtype Country struct {      // defining struct variables    Name      string    Capital   string    Continent string}  // main functionfunc main() {      // defining a struct instance    var country1 Country      // data in JSON format which    // is to be decoded    Data := []byte(`{        "Name": "India",          "Capital": "New Delhi",        "Continent": "Asia"    }`)      // decoding country1 struct    // from json format    err := json.Unmarshal(Data, &country1)      if err != nil {          // if error is not nil        // print error        fmt.Println(err)    }      // printing details of    // decoded data    fmt.Println("Struct is:", country1)    fmt.Printf("%s's capital is %s and it is in %s.\n", country1.Name,                                  country1.Capital, country1.Continent)}
Output:
Struct is: {India New Delhi Asia}
India's capital is New Delhi and it is in Asia.

Example 2:
// Golang program to illustrate the// concept of parsing JSON to an arraypackage main  import (    "encoding/json"    "fmt")  // declaring a structtype Country struct {      // defining struct variables    Name      string    Capital   string    Continent string}  // main functionfunc main() {      // defining a struct instance    var country []Country      // JSON array to be decoded    // to an array in golang    Data := []byte(`    [        {"Name": "Japan", "Capital": "Tokyo", "Continent": "Asia"},        {"Name": "Germany", "Capital": "Berlin", "Continent": "Europe"},        {"Name": "Greece", "Capital": "Athens", "Continent": "Europe"},        {"Name": "Israel", "Capital": "Jerusalem", "Continent": "Asia"}    ]`)      // decoding JSON array to    // the country array    err := json.Unmarshal(Data, &country)      if err != nil {          // if error is not nil        // print error        fmt.Println(err)    }      // printing decoded array    // values one by one    for i := range country {        fmt.Println(country[i].Name + " - " + country[i].Capital +                                      " - " + country[i].Continent)    }}
Output:
Japan - Tokyo - Asia
Germany - Berlin - Europe
Greece - Athens - Europe
Israel - Jerusalem - Asia

Go Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
strings.Replace() Function in Golang With Examples
fmt.Sprintf() Function in Golang With Examples
Arrays in Go
Golang Maps
How to Split a String in Golang?
Interfaces in Golang
Slices in Golang
Different Ways to Find the Type of Variable in Golang
How to convert a string in lower case in Golang?
How to Trim a String in Golang?