paint-brush
Golang - How to Parse JSON Data in Goby@jimmy315
16,044 reads
16,044 reads

Golang - How to Parse JSON Data in Go

by JimmyOctober 11th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In Go (also known as Golang), we can parse JSON data using the amazing `encoding/json` package. Here's a step-by-step guide on how to parse JSON in Go. Create a struct that represents the structure of the JSON you want to parse. Every field in the struct should have a corresponding JSON field tag to map it to the JSON keys. Use the `json.Unmarshal()` function to parse the JSON data into your provided Go struct.
featured image - Golang - How to Parse JSON Data in Go
Jimmy HackerNoon profile picture

In Go (also known as Golang), we can parse JSON data using the amazing encoding/json package, which provides functions for encoding and decoding JSON data. Here's a step-by-step guide on how to parse JSON in Go:

Step 1

Create a struct that represents the structure of the JSON you want to parse. Every field in the struct should have a corresponding JSON field tag to map it to the JSON keys.


type Person struct {
	Name  string `json:"name"`
	Age   int    `json:"age"`
	City  string `json:"city"`
	Email string `json:"email"`
}


Step 2

Use the json.Unmarshal() function encoding/json package to parse the JSON data into your provided Go struct.


	err := json.Unmarshal(jsonData, &person)


A Complete Example of How to Parse JSON in Go

package main

import (
	"encoding/json"
	"fmt"
)

// Struct to represent the JSON data
type Person struct {
	Name  string `json:"name"`
	Age   int    `json:"age"`
	City  string `json:"city"`
	Email string `json:"email"`
}


// main function
func main() {
	// JSON data as a byte slice
	jsonData := []byte(`{
		"name": "Jimmy",
		"age": 28,
		"city": "New York",
		"email": "[email protected]"
	}`)

	// Create an instance of the struct to hold the parsed data
	var person Person

	// Parse the JSON data into the struct
	err := json.Unmarshal(jsonData, &person)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	// Access the parsed data
	fmt.Println("Name:", person.Name)
	fmt.Println("Age:", person.Age)
	fmt.Println("City:", person.City)
	fmt.Println("Email:", person.Email)
}


Online Tool to parse JSON data: If you're looking to parse, you can use an online JSON Formatter.


In the above example, we defined a Person struct that represents the JSON data. Fields in the struct are tagged with the corresponding JSON keys. Then we use json.Unmarshal() to perform parsing JSON data and assign it to person variable.


When parsing is done using the above approach, you can access individual properties of person struct to work with JSON data, and make sure to handle errors properly where something goes wrong or provides JSON is not valid as per struct's structure.

Time Complexity of json.Unmarshal

The json.Unmarshal function needs to iterate through each byte in the JSON data in order to be parsed.


If we discuss the time complexity of json.Unmarshal, it is generally O(n). Where n is the size of the provided JSON data.


Explore the encoding/json package`s official documentation and more details about it on pkg.go.dev.