Re: [go-nuts] JSON: Having trouble parsing map keys with dots like {"foo.bar":"baz"}

2020-01-29 Thread Mark Hansen
Ah, I see what's going on: Go is mapping the "qux" field to the Qux struct
arg just because they're case-insensitively equal. Go's totally ignoring my
invalid struct tag.

D'oh, I was totally holding it wrong. Thanks everyone! :-)

On Wed, 29 Jan 2020 at 08:12, Mark Hansen  wrote:

> Ah, thanks! Thought I might be holding it wrong.
>
> Still though, isn’t it strange that the extra space works if you don’t
> have the dot in the key?
>
>
>
> On Wed, 29 Jan 2020 at 00:59, Lutz Horn  wrote:
>
>> Remove the blank in ``json: "baz.bar"` and make it `json:"baz.bar"`. This
>> works: https://play.golang.com/p/i9SURYgGO66
>>
>> 
>> Von: golang-nuts@googlegroups.com  im
>> Auftrag von m...@markhansen.co.nz 
>> Gesendet: Dienstag, 28. Januar 2020 12:14
>> An: golang-nuts
>> Betreff: [go-nuts] JSON: Having trouble parsing map keys with dots like
>> {"foo.bar":"baz"}
>>
>> Hi folks, for background, I'm trying to read the Kaiterra API<
>> https://www.kaiterra.com/dev/#overview> using encoding/json, which has
>> JSON values like this:
>>
>>
>> {"id":"-0001-0001--7e57c0de","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"`
>> Quxstring `json: "qux"`
>> }
>>
>> func TestDotKeyJsonParsing(t *testing.T) {
>> f := {}
>> 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: ""
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "golang-nuts" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to golang-nuts+unsubscr...@googlegroups.com> golang-nuts+unsubscr...@googlegroups.com>.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/golang-nuts/539f857c-d96a-45af-9a74-c328753bd12d%40googlegroups.com
>> <
>> https://groups.google.com/d/msgid/golang-nuts/539f857c-d96a-45af-9a74-c328753bd12d%40googlegroups.com?utm_medium=email_source=footer
>> >.
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAM07o6TiccrXvLCTeDk%3DWS5akcBZ32LiFmDW95gfV4PAmgnLQQ%40mail.gmail.com.


Re: [go-nuts] JSON: Having trouble parsing map keys with dots like {"foo.bar":"baz"}

2020-01-28 Thread Mark Hansen
Ah, thanks! Thought I might be holding it wrong.

Still though, isn’t it strange that the extra space works if you don’t have
the dot in the key?



On Wed, 29 Jan 2020 at 00:59, Lutz Horn  wrote:

> Remove the blank in ``json: "baz.bar"` and make it `json:"baz.bar"`. This
> works: https://play.golang.com/p/i9SURYgGO66
>
> 
> Von: golang-nuts@googlegroups.com  im
> Auftrag von m...@markhansen.co.nz 
> Gesendet: Dienstag, 28. Januar 2020 12:14
> An: golang-nuts
> Betreff: [go-nuts] JSON: Having trouble parsing map keys with dots like
> {"foo.bar":"baz"}
>
> Hi folks, for background, I'm trying to read the Kaiterra API<
> https://www.kaiterra.com/dev/#overview> using encoding/json, which has
> JSON values like this:
>
>
> {"id":"-0001-0001--7e57c0de","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"`
> Quxstring `json: "qux"`
> }
>
> func TestDotKeyJsonParsing(t *testing.T) {
> f := {}
> 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: ""
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com golang-nuts+unsubscr...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/539f857c-d96a-45af-9a74-c328753bd12d%40googlegroups.com
> <
> https://groups.google.com/d/msgid/golang-nuts/539f857c-d96a-45af-9a74-c328753bd12d%40googlegroups.com?utm_medium=email_source=footer
> >.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAM07o6SFwTWOqGZ_7mwqFFa03hqvdHB9HZePMGp2Xr1EbvA1mQ%40mail.gmail.com.


Re: [go-nuts] JSON: Having trouble parsing map keys with dots like {"foo.bar":"baz"}

2020-01-28 Thread Rich
All of these are good suggestions -- but when working with JSON I find this 
package to be very helpful: 

https://github.com/hjson/hjson-go  

I use json as a config file for my programs and when users fat finger the 
config, forget a comma or put a space in the wrong place -- this for the 
most part fixes those minor blunders.  I've also found that even though 
another system generated the JSON, it might not be fully compliant with 
what I wrote, hjson-go has bailed me out on more than one project. 



On Tuesday, January 28, 2020 at 9:59:51 AM UTC-5, Jake Montgomery wrote:
>
> FYI - "go vet" will catch this. Running "go vet" is always a good idea.
>
> On Tuesday, January 28, 2020 at 9:00:05 AM UTC-5, Lutz Horn wrote:
>>
>> Remove the blank in ``json: "baz.bar"` and make it `json:"baz.bar"`. This 
>> works: https://play.golang.com/p/i9SURYgGO66 
>>
>>  
>> Von: golan...@googlegroups.com  im Auftrag 
>> von ma...@markhansen.co.nz  
>> Gesendet: Dienstag, 28. Januar 2020 12:14 
>> An: golang-nuts 
>> Betreff: [go-nuts] JSON: Having trouble parsing map keys with dots like 
>> {"foo.bar":"baz"} 
>>
>> Hi folks, for background, I'm trying to read the Kaiterra API<
>> https://www.kaiterra.com/dev/#overview> using encoding/json, which has 
>> JSON values like this: 
>>
>> {"id":"-0001-0001--7e57c0de","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"` 
>> Quxstring `json: "qux"` 
>> } 
>>
>> func TestDotKeyJsonParsing(t *testing.T) { 
>> f := {} 
>> 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: "" 
>>
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/36e6c19c-77e2-40cc-91b4-d155085564c1%40googlegroups.com.


Re: [go-nuts] JSON: Having trouble parsing map keys with dots like {"foo.bar":"baz"}

2020-01-28 Thread Jake Montgomery
FYI - "go vet" will catch this. Running "go vet" is always a good idea.

On Tuesday, January 28, 2020 at 9:00:05 AM UTC-5, Lutz Horn wrote:
>
> Remove the blank in ``json: "baz.bar"` and make it `json:"baz.bar"`. This 
> works: https://play.golang.com/p/i9SURYgGO66 
>
>  
> Von: golan...@googlegroups.com   > im Auftrag von ma...@markhansen.co.nz  <
> ma...@markhansen.co.nz > 
> Gesendet: Dienstag, 28. Januar 2020 12:14 
> An: golang-nuts 
> Betreff: [go-nuts] JSON: Having trouble parsing map keys with dots like 
> {"foo.bar":"baz"} 
>
> Hi folks, for background, I'm trying to read the Kaiterra API<
> https://www.kaiterra.com/dev/#overview> using encoding/json, which has 
> JSON values like this: 
>
> {"id":"-0001-0001--7e57c0de","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"` 
> Quxstring `json: "qux"` 
> } 
>
> func TestDotKeyJsonParsing(t *testing.T) { 
> f := {} 
> 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: "" 
>
>
> -- 
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group. 
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golan...@googlegroups.com  golang-nuts+unsubscr...@googlegroups.com >. 
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/539f857c-d96a-45af-9a74-c328753bd12d%40googlegroups.com
> <
> https://groups.google.com/d/msgid/golang-nuts/539f857c-d96a-45af-9a74-c328753bd12d%40googlegroups.com?utm_medium=email_source=footer>.
>  
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/03ebed61-a185-4509-b52d-f3b8e8934153%40googlegroups.com.


AW: [go-nuts] JSON: Having trouble parsing map keys with dots like {"foo.bar":"baz"}

2020-01-28 Thread Lutz Horn
Remove the blank in ``json: "baz.bar"` and make it `json:"baz.bar"`. This 
works: https://play.golang.com/p/i9SURYgGO66


Von: golang-nuts@googlegroups.com  im Auftrag von 
m...@markhansen.co.nz 
Gesendet: Dienstag, 28. Januar 2020 12:14
An: golang-nuts
Betreff: [go-nuts] JSON: Having trouble parsing map keys with dots like 
{"foo.bar":"baz"}

Hi folks, for background, I'm trying to read the Kaiterra 
API<https://www.kaiterra.com/dev/#overview> using encoding/json, which has JSON 
values like this:

{"id":"-0001-0001--7e57c0de","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"`
Quxstring `json: "qux"`
}

func TestDotKeyJsonParsing(t *testing.T) {
f := {}
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: ""


--
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
golang-nuts+unsubscr...@googlegroups.com<mailto:golang-nuts+unsubscr...@googlegroups.com>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/539f857c-d96a-45af-9a74-c328753bd12d%40googlegroups.com<https://groups.google.com/d/msgid/golang-nuts/539f857c-d96a-45af-9a74-c328753bd12d%40googlegroups.com?utm_medium=email_source=footer>.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/AM7P191MB1060C7A5A59B82BF870605209E0A0%40AM7P191MB1060.EURP191.PROD.OUTLOOK.COM.


[go-nuts] JSON: Having trouble parsing map keys with dots like {"foo.bar":"baz"}

2020-01-28 Thread mark
Hi folks, for background, I'm trying to read the Kaiterra API 
 using encoding/json, which has 
JSON values like this:

{"id":"-0001-0001--7e57c0de","*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"`
Quxstring `json: "qux"`
}

func TestDotKeyJsonParsing(t *testing.T) {
f := {}
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: ""

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/539f857c-d96a-45af-9a74-c328753bd12d%40googlegroups.com.