Hi folks, for background, I'm trying to read the
Kaiterra API using encoding/json, which has JSON values like this:
{"id":"00000000-0001-0001-0000-00007e57c0de","info.aqi":{"ts":"2018-03-26T08:53:20Z","data":{"pm10":120,"pm25":214}}}
I'm having trouble parsing the "info.aqi" field using encoding/json. I set the "info.aqi" field as a struct tag `json: "info.aqi"`, but the struct is just empty after parsing.
It seems more likely I'm holding it wrong, but I'm wondering if perhaps this is a bug in Go's JSON parsing?
I thought I'd make a minimal repro test, which fails:
package main
import (
"encoding/json"
"strings"
"testing"
)
type Foo struct {
BazBar string `json: "baz.bar"`
Qux string `json: "qux"`
}
func TestDotKeyJsonParsing(t *testing.T) {
f := &Foo{}
d := json.NewDecoder(strings.NewReader(`{"baz.bar": "hello", "qux": "hi"}`))
err := d.Decode(f)
if err != nil {
t.Fatalf("json decoding failed: %v", err)
}
if f.Qux != "hi" {
t.Fatalf("Expected f.Qux to be hi")
}
if f.BazBar != "hello" {
t.Errorf("wanted: hello, got: %q", f.BazBar)
}
}
And the Qux field passes fine, but the BazBar field is not set, so the test fails there:
--- FAIL: TestDotKeyJsonParsing (0.00s)
/Users/mark/projects/godot/dot_test.go:26: wanted: hello, got: ""