I'm trying to check if a Json string matches an Avro schema. I don't care about doing serialization of the data, just getting a bool result of isValidJson=true/false
. I'll go with every golang library. I've tried to write something with this goavro lib, but it didn't work for me, maybe because I'm new to go like.
Desired pseudo code:
func main() {
avroSchema :=
`{"type":"record","name":"raw","namespace":"events","fields":[{"name":"my_int","type":["null","int"],"default":null},{"name":"my_string","type":["null","string"],"default":"null"},{"name":"my_string2","type":null}]}`
jsonString := `{"my_int": 3, "my_string": "foo", "my_string2": null}`
ok ;= isValidJson(jsonString, avroSchema)
}
Any idea how to implement the isValidJson(..)
method?
Your schema json is invalid, it's missing the terminating }
, so goavro.NewCodec
returns an error.
Then your json string definitely doesn't match the schema, the json values must be a {type: value}
.
You can use the following corrected schema and example string to validate it.
func main() {
avroSchema := `
{
"type":"record",
"name":"raw",
"namespace":"events",
"fields":[
{
"name":"my_int",
"type":[
"null",
"int"
],
"default":null
},
{
"name":"my_string",
"type":[
"null",
"string"
],
"default":null
},
{
"name":"my_string2",
"type":"null"
}
]
}`
codec, err := goavro.NewCodec(avroSchema)
if err != nil {
log.Fatalf("Codec error: %v", err)
}
jsonString := `{"my_int": {"int":3}, "my_string": {"string":"foo"}, "my_string2": null}`
decoded, _, err := codec.NativeFromTextual([]byte(jsonString))
if err != nil {
log.Fatalf("NativeFromTextual error: %v", err)
}
log.Println("Decoded:", decoded)
}
This prints:
Decoded: map[my_int:map[int:3] my_string:map[string:foo] my_string2:]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.