Home > Backend Development > Golang > How to Differentiate Between Null and Absent JSON Fields in Go?

How to Differentiate Between Null and Absent JSON Fields in Go?

Barbara Streisand
Release: 2024-12-05 08:57:09
Original
781 people have browsed it

How to Differentiate Between Null and Absent JSON Fields in Go?

Distinguishing Between JSON Fields Set to Null and Fields Not Present

When unmarshaling JSON into a Go struct, discerning whether a field is null or simply absent can be a challenge. Both scenarios result in a nil value within the struct, making it difficult to determine the field's original presence.

To address this issue, let's explore two solutions:

1. Using Generics (Go 1.18 )

Go 1.18 introduces generics, allowing us to create a generic struct that tracks the presence of JSON values:

type Optional[T any] struct {
    Defined bool
    Value   *T
}
Copy after login

This Optional struct uses a Defined field to indicate whether the value exists in the JSON payload. When the field is present, the Value field holds the unmarshaled value. By incorporating this struct into your own structs, you can distinguish between null and absent fields:

type Payload struct {
    Field1 Optional[string] `json:"field1"`
    Field2 Optional[bool]   `json:"field2"`
    Field3 Optional[int32]  `json:"field3"`
}
Copy after login

2. Custom Type (Pre-Generics)

Before generics, we can create a custom type to handle optional values:

type OptionalString struct {
    Defined bool
    Value   *string
}
Copy after login

This type includes a Defined field to indicate presence and a Value field to hold the unmarshaled string. By defining a custom type for each type you need to handle (e.g., OptionalInt for integers), you can retain the information about field presence.

Examples:

Consider the following JSON payloads:

{
  "somefield1":"somevalue1",
  "somefield2":null
}
Copy after login
{
  "somefield1":"somevalue1",
}
Copy after login

Using our solutions, we can differentiate between these cases:

p1 := Payload{}
_ = json.Unmarshal([]byte(`{
            "somefield1": "somevalue1",
            "somefield2": null
        }`), &p1)

fmt.Printf("Should be defined == true and value == nil: \n%+v\n\n", p1)
Copy after login

Output:

Should be defined == true and value == nil: 
{SomeField1:somevalue1 SomeField2:{Defined:true Value:<nil>}}
Copy after login
p2 := Payload{}
_ = json.Unmarshal([]byte(`{
            "somefield1": "somevalue1",
        }`), &p2)

fmt.Printf("Should be defined == false: \n%+v\n\n", p2)
Copy after login

Output:

Should be defined == false: 
{SomeField1:somevalue1 SomeField2:{Defined:false Value:<nil>}}
Copy after login

These examples demonstrate how our solutions can distinguish between null and absent JSON fields, providing valuable information during deserialization.

The above is the detailed content of How to Differentiate Between Null and Absent JSON Fields 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