Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/json: Unmarshal of json elements into struct with variable "uuid" in lower case does not work #24849

Closed
Geekgasm opened this issue Apr 13, 2018 · 1 comment

Comments

Projects
None yet
3 participants
@Geekgasm
Copy link

commented Apr 13, 2018

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.10.1 darwin/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/d037698/Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/d037698/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.10.1/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.10.1/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/xp/4bmz5sv10cd8hp6r9frr9__80000gn/T/go-build330651932=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Run this program to unmarshal a json blob with a key "uuid":

package main

import (
  "fmt"
  "encoding/json"
)

func main() {
  var jsonBlob = []byte(`[
    {"UUID": "C5FA164A3F4C11E88B5406B45C771ECC"},
    {"uuid": "C5FA164A3F4C11E88B5406B45C771ECC"}
  ]`)

  type BiglyUUID struct {
    UUID  string
  }
  var bigUuids []BiglyUUID
  err := json.Unmarshal(jsonBlob, &bigUuids)
  if err != nil {
    fmt.Println("error:", err)
  }
  fmt.Printf("%+v", bigUuids)
  fmt.Println()

  type tinyUuid struct {
    uuid string
  }
  var smallUuids []tinyUuid
  err2 := json.Unmarshal(jsonBlob, &smallUuids)
  if err2 != nil {
    fmt.Println("error:", err2)
  }
  fmt.Printf("%+v", smallUuids)
  fmt.Println()
}

Go Playground link: https://play.golang.org/p/pVDWjEWgCCQ

What did you expect to see?

[{UUID:C5FA164A3F4C11E88B5406B45C771ECC} {UUID:C5FA164A3F4C11E88B5406B45C771ECC}]
[{uuid:C5FA164A3F4C11E88B5406B45C771ECC} {uuid:C5FA164A3F4C11E88B5406B45C771ECC}]

What did you see instead?

[{UUID:C5FA164A3F4C11E88B5406B45C771ECC} {UUID:C5FA164A3F4C11E88B5406B45C771ECC}]
[{uuid:} {uuid:}]

@Geekgasm Geekgasm changed the title encoding/json: Unmarshal of json elements with key "uuid" in lower case does not work encoding/json: Unmarshal of json elements into struct with variable "uuid" in lower case does not work Apr 13, 2018

@bradfitz

This comment has been minimized.

Copy link
Member

commented Apr 13, 2018

This is working as documented. Read the encoding/json docs for details.

For questions about Go, see https://golang.org/wiki/Questions.

@bradfitz bradfitz closed this Apr 13, 2018

@golang golang locked and limited conversation to collaborators Apr 13, 2019

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.