Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

json
golang
socket programming

How to use JSON in Go

Educative Answers Team

JSON

JSON, short for JavaScript Object Notation, is a format in which data is stored and transferred. It is easy for humans to understand and write and easy for computers to parse. JSON is often used in web applications and socket programming for transmission over networks.

Implementation

To encode a JSON object in Go, the Marshal() method is used. To decode the resulting JSON objects, the Unmarshal() method is used​.

package main
import (
  "encoding/json"
  "fmt"
)

// Struct defined
type ShotExample struct {
  Name string
  Shot string
}

func main() {

  // Converting Integer to JSON
  intJson, err := json.Marshal(22)
  if err == nil { 
    fmt.Println(string(intJson))
  } else {
    panic(err)
  }

  // Converting String to JSON
  strJson, err := json.Marshal("Educative")
  if err == nil { 
    fmt.Println(string(strJson))
  } else {
    panic(err)
  }

  // Declaring a map
  mapVar := map[string]int{"num1": 1, "num2": 2}
  // Converting map to JSON
  mapJson, err := json.Marshal(mapVar)
  if err == nil { 
    fmt.Println(string(mapJson))
  } else {
    panic(err)
  }

  // Declaring struct object
  structVar := ShotExample{Name: "Educative", Shot: "JSON"}
  // Converting map to JSON
  structJson, err := json.Marshal(structVar)
  if err == nil { 
    fmt.Println(string(structJson))
  } else {
    panic(err)
  }
}

This example demonstrates some different data types being encoded into JSON objects. As can be seen, JSON can be used to encode individual data items, maps, and structs.

JSON objects need to be decoded before they are used for programming. To decode JSON objects in GO, you need to declare a container variable.

package main
import (
  "encoding/json"
  "fmt"
)

// Defining struct
type ShotExample struct {
  Name string
  Shot string
}
func main() {

  // Declaring map variable
  mapVar := map[string]int{"num1": 1, "num2": 2}
  // Converting to JSON
  mapJson, err := json.Marshal(mapVar)
  // Declaring container variable for decoding
  var mapDecode map[string]interface{}
  // Converting JSON object to map in container
  err = json.Unmarshal(mapJson, &mapDecode)
  if err == nil {
    fmt.Println(mapDecode)
  } else {
    panic(err)
  }

  // Declaring struct object
  structVar := ShotExample{Name: "Educative", Shot: "JSON"}
  // Converting struct to JSON
  structJson, err := json.Marshal(structVar)
  // Declaring container variable for decoding
  var structDecode ShotExample
  // Converting JSON object to struct in container
  err = json.Unmarshal(structJson, &structDecode)
  if err == nil { 
    fmt.Println(structDecode)
  } else {
    panic(err)
  }
}

This example shows how a JSON object is decoded for use in Go Language. A map variable, mapDecode, is declared with the value type defined as an interface to allow for different data types to act as values in the key-value pairs.

ShotExample struct type variable structDecode is initialized in a similar way; it stores JSON data of the same type.

RELATED TAGS

json
golang
socket programming
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring