You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by "Nandor Kollar (JIRA)" <ji...@apache.org> on 2017/12/07 15:25:00 UTC

[jira] [Commented] (AVRO-2107) Unable to deserialize JSON with array type to GenericRecord

    [ https://issues.apache.org/jira/browse/AVRO-2107?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16281999#comment-16281999 ] 

Nandor Kollar commented on AVRO-2107:
-------------------------------------

[~darshanmehta2] I think the problem is that the type of sampleArray in your schema is [union|http://avro.apache.org/docs/current/spec.html#Unions], with only one brach. If you'd like store a union of different elements (like string or a record type), then sampleArray in your JSON data should look like this:
{code}
			"sampleArray": [
				{
					"a.b.sampleArrayRecord": {
						"sampleArrayRecordString": "KLMNO"
					}
				}
			]
{code}

I guess your intention is not this though, you'd like to store just records of a certain type in the array, and nothing else. In this case you shouldn't make elements of the array union, simply drop "\[" and "]" near "items":
{code}
                     "name":"sampleArray",
                     "type":{
                       "type":"array",
                       "items":
                     	{
                     	  "name":"sampleArrayRecord",
                     	  "type":"record",
                     	  "fields":[
                     		{
                     		  "name":"sampleArrayRecordString",
                     		  "type":"string",
                     		  "doc":"Sample array record string",
                     		  "omdFieldSecurity":"internal",
                     		  "omdLogicalDataType":"string"
                     		}
                     	  ],
                     	  "doc":"Sample array record",
                     	  "omdFileSecurity":"internal",
                     	  "omdRepositoryTag":"1.4.0"
                     	}
                     },
{code}

> Unable to deserialize JSON with array type to GenericRecord
> -----------------------------------------------------------
>
>                 Key: AVRO-2107
>                 URL: https://issues.apache.org/jira/browse/AVRO-2107
>             Project: Avro
>          Issue Type: Bug
>    Affects Versions: 1.8.1, 1.8.2
>            Reporter: Darshan Mehta
>         Attachments: sample.json, test-schema.avsc
>
>
> I am trying to deserialise the json with array type into GenericRecord and I get the following exception:
> {code:java}
> Exception in thread "main" org.apache.avro.AvroTypeException: Unknown union branch sampleArrayRecordString
> 	at org.apache.avro.io.JsonDecoder.readIndex(JsonDecoder.java:445)
> 	at org.apache.avro.io.ResolvingDecoder.doAction(ResolvingDecoder.java:290)
> 	at org.apache.avro.io.parsing.Parser.advance(Parser.java:88)
> 	at org.apache.avro.io.ResolvingDecoder.readIndex(ResolvingDecoder.java:267)
> 	at org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:179)
> 	at org.apache.avro.generic.GenericDatumReader.readArray(GenericDatumReader.java:266)
> 	at org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:177)
> 	at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:153)
> 	at org.apache.avro.generic.GenericDatumReader.readField(GenericDatumReader.java:232)
> 	at org.apache.avro.generic.GenericDatumReader.readRecord(GenericDatumReader.java:222)
> 	at org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:175)
> 	at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:153)
> 	at org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:179)
> 	at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:153)
> 	at org.apache.avro.generic.GenericDatumReader.readField(GenericDatumReader.java:232)
> 	at org.apache.avro.generic.GenericDatumReader.readRecord(GenericDatumReader.java:222)
> 	at org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:175)
> 	at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:153)
> 	at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:145)
> 	at a.b.c.d.Test2.main(Test2.java:19)
> {code}
> Below is the code:
> {code:java}
> final Schema schema = Schema.parse(new File(
> 		"<schema_file_path>"));
> byte[] payload = Files.readAllBytes(Paths.get(
> 		"<json_file_path>"));
> GenericRecord record = new GenericDatumReader<GenericRecord>(schema).read(null,
> 		DecoderFactory.get().jsonDecoder(schema, new String(payload)));
> System.out.println(record);
> {code}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)