Home > Backend Development > Golang > How to Avoid Double Unmarshaling When Parsing JSON in Go?

How to Avoid Double Unmarshaling When Parsing JSON in Go?

Mary-Kate Olsen
Release: 2024-12-30 07:07:09
Original
1022 people have browsed it

How to Avoid Double Unmarshaling When Parsing JSON in Go?

Unmarshalling JSON in Go Without Double Unmarshaling

In Go, it's possible to face a situation where you need to parse JSON into various structs, but double unmarshalling occurs, leading to redundancy. This article presents a solution that involves partially unmarshalling the data to avoid the second unmarshaling step.

Understanding the Issue

When attempting to parse JSON into known structs, a common approach is:

  1. Unmarshal JSON into a generic map[string]interface{}
  2. Find the expected key
  3. Attempt to cast the value to the desired type
  4. If casting fails, marshal the value as JSON and unmarshal it to the desired type (double unmarshalling)

The Solution

Instead of casting the value to an interface{}, one can unmarshal it into a json.RawMessage. This partially unmarshals the data and allows subsequent unmarshalling to specific structs based on the key value.

Sample Code

import (
    "encoding/json"
    "fmt"
)

type Ping struct {
    Ping string `json:"ping"`
}

type Ack struct {
    Messages []Message `json:"messages"`
}

type Message string

func main() {
    testJSON := []byte(`{"ack":{"messages":["Hi there","Hi again"]}}`)
    var myAck = Ack{}
    var myMap map[string]json.RawMessage

    err := json.Unmarshal(testJSON, &myMap)
    if err != nil {
        fmt.Println("error unmarshalling: ", err)
    }

    jsonAck, ok := myMap["ack"]
    if !ok {
        fmt.Println("key 'ack' not found")
        return
    }

    err = json.Unmarshal(jsonAck, &myAck)
    if err != nil {
        fmt.Println("unmarshal error", err)
    } else {
        fmt.Println("New ack object: ", myAck)
    }
}
Copy after login

In this modified code, we replace the map[string]interface{} with map[string]json.RawMessage. When handling the "ack" key, we unmarshal directly to the Ack struct, eliminating the need for double unmarshalling.

The above is the detailed content of How to Avoid Double Unmarshaling When Parsing JSON in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template