Golang - How to Parse JSON Data in Go

Written by jimmy315 | Published 2023/10/11
Tech Story Tags: web-development | golang | golang-api | json-parser | parse-json-data | programming-tutorial | programming-tools | json-guide-for-beginners

TLDRIn 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.via the TL;DR App

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.


Written by jimmy315 | I am a passionate and innovative software developer.
Published by HackerNoon on 2023/10/11