Skip to content
Related Articles

Related Articles

Improve Article

How to Parse JSON in Golang?

  • Difficulty Level : Basic
  • Last Updated : 10 Jul, 2020
Geek Week

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 struct
package main
  
import (
    "encoding/json"
    "fmt"
)
  
// declaring a struct
type Country struct {
  
    // defining struct variables
    Name      string
    Capital   string
    Continent string
}
  
// main function
func 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 array
package main
  
import (
    "encoding/json"
    "fmt"
)
  
// declaring a struct
type Country struct {
  
    // defining struct variables
    Name      string
    Capital   string
    Continent string
}
  
// main function
func 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



My Personal Notes arrow_drop_up
Recommended Articles
Page :