Skip to content

Latest commit

 

History

History
84 lines (60 loc) · 1.83 KB

File metadata and controls

84 lines (60 loc) · 1.83 KB

Go Sample Example - Basic JSON Decoding

This repository demonstrates how to decode JSON strings into Go structs using Go's standard library package encoding/json. It showcases how to unmarshal JSON data into Go structures and perform operations on the decoded data.

📖 Information

  • This example covers basic JSON decoding using Go structs and the `json.Unmarshal` function.
  • It demonstrates how to convert a JSON string into a Go struct, handle errors during decoding, and marshal the struct back into a formatted JSON string.

💻 Code Example

package main

import (
	"encoding/json"
	"fmt"
)

type Person1 struct {
	Name    string
	Age     int
	Country string
}

func main() {

	// This example demonstrates how to convert a JSON string into a Go struct using json.Unmarshal

	jsonString := `{"Name":"John","Age":30,"Country":"USA"}`

	var person Person1

	err := json.Unmarshal([]byte(jsonString), &person)
	if err != nil {
		fmt.Println("Error decoding JSON:", err)
		return
	}

	// Marshal the struct back to JSON format
	jsonOutput, err := json.MarshalIndent(person, "", "  ")
	if err != nil {
		fmt.Println("Error encoding JSON:", err)
		return
	}

	fmt.Println(string(jsonOutput))
}

🏃 How to Run

  1. Make sure you have Go installed. If not, you can download it from here.

  2. Clone this repository:

    git clone /~https://github.com/Rapter1990/go_sample_examples.git
  3. Navigate to the 002_basic_decoding_json directory:

    cd go_sample_examples/030_json/002_basic_decoding_json
  4. Run the Go program:

    go run 002_basic_decoding_json.go

📦 Output

When you run the program, you should see the following output:

{
  "Name": "John",
  "Age": 30,
  "Country": "USA"
}