This question already has an answer here:
Golang does not support case sensitive unmarshalling of JSON using the standard packages. This seems like a very common need though.
Is there any way to get a precise matching of cases when marshalling and unmarshalling JSON?
Example: I don't want to match "id" with "ID".
</div>
The standard library encoding/json does in fact support case sensitive encoding/decoding if your data structures define the members that way and include tags appropriately.
For example:
package main
import (
"encoding/json"
"fmt"
)
type image struct {
Url string `json:"url"`
}
type images struct {
Image1 image `json:"image"`
Image2 image `json:"Image"`
}
func main() {
i := images{Image1: image{Url: "test.jpg"}, Image2: image{Url: "test2.jpg"}}
data, err := json.Marshal(i)
if err != nil {
fmt.Printf("error: %s", err)
}
fmt.Println(string(data))
var i2 images
err = json.Unmarshal(data, &i2)
if err != nil {
fmt.Printf("error: %s", err)
}
fmt.Printf("%#v
", i2)
}
https://play.golang.org/p/GWUWYUc-T9t
Which will output:
{"image":{"url":"test.jpg"},"Image":{"url":"test2.jpg"}}
main.images{Image1:main.image{Url:"test.jpg"}, Image2:main.image{Url:"test2.jpg"}}
However, another good json encoding package is jsonparser: https://github.com/buger/jsonparser
I don't know if it supports case sensativity differentaly then the standard library package.