mholt / json-to-go

Translates JSON into a Go type in your browser instantly (original)
https://mholt.github.io/json-to-go/
MIT License
4.51k stars 475 forks source link

JSON to go is not working when json list has "null" values #77

Closed alok87 closed 4 years ago

alok87 commented 4 years ago

Program

https://play.golang.org/p/RCIvR0wGjvZ

package main

import (
    "fmt"
    "encoding/json"
)

// genereated using https://mholt.github.io/json-to-go/
type DebeziumSchema struct {
    Type        string   `json:"type"`
    Name        string   `json:"name"`
    Namespace   string   `json:"namespace"`
    Fields      []Fields `json:"fields"`
    ConnectName string   `json:"connect.name"`
}
type Fields struct {
    Name    string        `json:"name"`
    Type    []interface{} `json:"type"`
    Default interface{}   `json:"default,omitempty"`
}

func main() {
    jsonStr := `{"type":"record","name":"Envelope","namespace":"datapipe.inventory.customers","fields":[{"name":"before","type":["null",{"type":"record","name":"Value","fields":[{"name":"id","type":"int"},{"name":"first_name","type":"string"},{"name":"last_name","type":"string"},{"name":"email","type":"string"}],"connect.name":"datapipe.inventory.customers.Value"}],"default":null},{"name":"after","type":["null","Value"],"default":null},{"name":"source","type":{"type":"record","name":"Source","namespace":"io.debezium.connector.mysql","fields":[{"name":"version","type":"string"},{"name":"connector","type":"string"},{"name":"name","type":"string"},{"name":"ts_ms","type":"long"},{"name":"snapshot","type":[{"type":"string","connect.version":1,"connect.parameters":{"allowed":"true,last,false"},"connect.default":"false","connect.name":"io.debezium.data.Enum"},"null"],"default":"false"},{"name":"db","type":"string"},{"name":"table","type":["null","string"],"default":null},{"name":"server_id","type":"long"},{"name":"gtid","type":["null","string"],"default":null},{"name":"file","type":"string"},{"name":"pos","type":"long"},{"name":"row","type":"int"},{"name":"thread","type":["null","long"],"default":null},{"name":"query","type":["null","string"],"default":null}],"connect.name":"io.debezium.connector.mysql.Source"}},{"name":"op","type":"string"},{"name":"ts_ms","type":["null","long"],"default":null},{"name":"transaction","type":["null",{"type":"record","name":"ConnectDefault","namespace":"io.confluent.connect.avro","fields":[{"name":"id","type":"string"},{"name":"total_order","type":"long"},{"name":"data_collection_order","type":"long"}]}],"default":null}],"connect.name":"datapipe.inventory.customers.Envelope"}
`
    var schema DebeziumSchema
    err := json.Unmarshal([]byte(jsonStr), &schema)
    if err != nil {
        panic(err)
    }

    fmt.Printf("%+v\n", schema)
}

Error

panic: json: cannot unmarshal object into Go struct field Fields.fields.type of type []interface {}

More Info: https://stackoverflow.com/questions/63564543/decode-a-debeuzium-event-schema-into-a-meaningful-datastructure-in-golang

Question

Please suggest why such jsons are not converting and would be right go type for this scenario?

mholt commented 4 years ago

The array has different kinds of values, strings and objects, so decoding it properly isn't obvious because there is no one type that fits it, you have to use type assertions to get what you want or implement a custom unmarshaler.

Closing, as this is more a question about Go than this project.