-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Unmarshal doesn't work for json keys with _ #498
Copy link
Copy link
Closed
Description
Hi, so it seems that Unmarshal won't work with JSON keys, that have underscores. Here's some code:
package main
import (
"bytes"
"fmt"
"github.com/spf13/viper"
)
type ConfigUnderscores struct {
FooBar string `json:"foo_bar"`
}
type Config struct {
FooBar string `json:"fooBar"`
}
var content1 = []byte(`
{
"foo_bar": "foo"
}`)
var content2 = []byte(`
{
"fooBar": "foo"
}`)
func main() {
var c1 ConfigUnderscores
var c2 Config
viper.SetConfigType("json")
viper.ReadConfig(bytes.NewReader(content1))
viper.Unmarshal(&c1)
fmt.Printf("ConfigUnderscores: %v\n", c1)
viper.ReadConfig(bytes.NewReader(content2))
viper.Unmarshal(&c2)
fmt.Printf("Config: %v\n", c2)
}
Output:
ConfigUnderscores: {}
Config: {foo}
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels