Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON to Avro Decoding - AvroTypeException: Expected field name not found

I am using Avro 1.8.2 and I am trying to convert JSON into a GenericRecord

 DatumReader<GenericData.Record> datumReader = new GenericDatumReader<>(schema);
 Decoder decoder = DecoderFactory.get().jsonDecoder(schema, jsonStr);
 datumReader.read(null, decoder)

I get JSON data from a third party and I have no control over the elements. The AVRO schema is

{
    "namespace":"com.avro.generated",
    "type":"record",
    "name":"TestEvent",
    "fields":[
        {"name":"userId","type":"string"},
        {"name":"frm","type":"string"},
        {"name":"issuerName","type":"string"},
        {"name":"profileId","type":"string"}
    ]
}

If I use this JSON

{
    "userId":"5435tert34tgcb21391f7bda71",
    "frm":"somerm",
    "issuerName":"somenameorts",
    "profileId":"0werwerwer0000-0000-000000000000"
}

It works fine. However if the json does not contain the frm element as shown below

{
    "userId":"5435tert34tgcb21391f7bda71",
    "issuerName":"somenameorts",
    "profileId":"0werwerwer0000-0000-000000000000"
}

Then I get this exception

org.apache.avro.AvroTypeException: Expected field name not found: frm.

Is there any way to make this work?. I have no control over the JSON. I have read other SO posts about using schemas like

{"name":"frm","type":["null","string"],"default": "null"}

But none of this is working

Thanks

like image 518
Jeenson Ephraim Avatar asked Oct 28 '25 06:10

Jeenson Ephraim


1 Answers

All fields are mandatory in AVRO, but you can provide a default so that it has the field.

{
    "namespace":"com.avro.generated",
    "type":"record",
    "name":"TestEvent",
    "fields":[
        {"name":"userId","type":["null","string"], "default": null},
        {"name":"frm","type":["null","string"], "default": null},
        {"name":"issuerName","type":["null","string"], "default": null},
        {"name":"profileId","type":["null","string"], "default": null}
    ]
}

EDIT: sorry didn't read the end of your message. What is the error when you say it is not working (also, notice that null is not to be quoted)

like image 106
BenoitParis Avatar answered Oct 30 '25 10:10

BenoitParis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!