I want to output an error if the field in json contains a value of null. How can I do it? I have tried "encoding/json". Maybe I need another library.
Code example:
package main
import (
"encoding/json"
"fmt"
"strings"
)
type Item struct {
Value *int
}
func main() {
var jsonBlob = `[
{},
{"Value": null},
{"Value": 0},
{"Value": 1}
]`
var items []Item
err := json.NewDecoder(strings.NewReader(jsonBlob)).Decode(&items)
if err != nil {
fmt.Println("error:", err)
}
for _, a := range items {
if a.Value != nil {
fmt.Println(*a.Value)
} else {
fmt.Println(a.Value)
}
}
}
I got:
<nil>
<nil>
0
1
I want:
<nil>
<error>
0
1
Please help. Many thanks!