The schemas are fine, but the JSON snippet isn't a valid instance of the second schema.
In the default JSON encoding for Avro, you have to include the name of the record as an object field[1]. For example, given test_schema_0.avsc with your first schema and test_schema_1.avsc as your second, here are random example instances: $ java -jar avro-tools-1.9.0-SNAPSHOT.jar random --count 1 --schema-file test_schema_0.avsc schema_0_random.avro log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. test.seed=1472871710806 $ java -jar avro-tools-1.9.0-SNAPSHOT.jar tojson --pretty schema_0_random.avro log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. { "name" : "msbvsjefb", "id" : 5742171927645279316 } $ java -jar avro-tools-1.9.0-SNAPSHOT.jar random --count 1 --schema-file test_schema_1.avsc schema_1_random.avro log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. test.seed=1472871721099 $ java -jar avro-tools-1.9.0-SNAPSHOT.jar tojson --pretty schema_1_random.avro log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. { "com.user.user_record" : { "name" : "ljfijs", "id" : -7695450471550075616 } } [1]: http://avro.apache.org/docs/current/spec.html#json_encoding On Fri, Sep 2, 2016 at 5:01 PM, Kamesh Kompella <kam...@chooxy.com> wrote: > Hi there, > First, please look at the following schema > > {"name": "user_record", > "namespace": "com.user", > "type": "record", > "fields" : [ > {"name": "name", "type": "string"}, > {"name": "id", "type": "long"} > ]} > > and the following JSON: > > {"name": “Foo", “id": 42} > > > When I run avro-tools with the option fromjson, I get a .avro file. Stuff > works. > > If I enclose the schema above into array as shown below (I bolded the array > begin and end in red for clarity), avro-tools (version 1.8.1) throws the > following exception and dies. > > > [{"name": "user_record", > "namespace": "com.user", > "type": "record", > "fields" : [ > {"name": "name", "type": "string"}, > {"name": "id", "type": "long"} > ]}] > > I get the following exception: > > Exception in thread "main" org.apache.avro.AvroTypeException: Unknown union > branch name at > org.apache.avro.io.JsonDecoder.readIndex(JsonDecoder.java:445) > > Does it make sense to enclose a schema into array? Is this a bug in > avro-tools or is this an invalid schema? The exception above seems to > indicate that a schema file may not begin with a JSON array of schemas. > > The documentation seems to indicate schema may be defined as union of other > other schemas. > > I cloned the code base and I could not locate a single instance of avsc file > in it that defined its schema as a JSON array. Hence, the question. > > I appreciate your response. > > Regards > Kamesh -- busbey