validation - How do I use an Avro schema to validate JSON? -


i'd know feasibility of using avro schema validate json comes app. in this post, doug cutting suggests using jsontofrag tool comes avro-tools jar. example trivial 1 of json "document" number:

echo 2 | java -jar avro-tools.jar jsontofrag '"int"' - | java -jar avro-tools.jar fragtojson '"int"' -  

while works, i'd know how more interesting json doc.

when try example json doc , schema on avro website fails, so:

the avro schema:

{"namespace": "example.avro",  "type": "record",  "name": "user",  "fields": [      {"name": "name", "type": "string"},      {"name": "favorite_number",  "type": ["int", "null"]},      {"name": "favorite_color", "type": ["string", "null"]}  ] } 

example json doc

{"name": "ben",  "favorite_number": 7,  "favorite_color": "red"} 

but when try with:

cat user.json | java -jar avro-tools.jar jsontofrag user.avsc - | java -jar avro-tools.jar fragtojson user.avsc - 

it error (stack trace elided):

exception in thread "main" org.apache.avro.schemaparseexception: org.codehaus.jackson.jsonparseexception:  unexpected character ('u' (code 117)):  expected valid value (number, string, array, object, 'true', 'false' or 'null')  @ [source: java.io.stringreader@74dca977; line: 1, column: 2] 

any ideas on how make work? or way use avro schema validate json?

the usage (and backtrace) jsontofrag tool leaves desired; means "schema" literal schema string, not filename containing schema. (surprise!) following tweak command worked me:

cat user.json | java -jar avro-tools.jar jsontofrag "`cat user.avsc`" - | java -jar avro-tools.jar fragtojson "`cat user.avsc`" - 

here i've used old-style backtics command-substitution; newer "$(cat user.avsc)" syntax works in bash , in other modern(ish) shells.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -