Search code examples
javajsonapache-kafkaschemaavro

JSON to Avro conversion exception - Expected start-union. Got VALUE_STRING


I have the following avsc (Avro schema) :

{
  "type": "record",
  "name": "DataEventId",
  "fields": [
    {
      "name": "redeliveredDataEventIndices",
      "type": { "type": "array", "items": "int" },
      "doc" : "Data event indices",
      "default": []
    },
  ],
  "namespace": "com.xxx.xxx.xxx"
}

When i try to convert json to avro with this Schema i get the following error :

org.apache.avro.AvroTypeException: Expected start-union. Got VALUE_STRING

My input data :

{"redeliveredDataEventIndices":"[]"}

I know this is a duplicate of How to fix Expected start-union. Got VALUE_NUMBER_INT when converting JSON to Avro on the command line? but how to give input for the type array (redeliveredDataEventIndices is array of type int in this case)


Solution

  • Your input data has the array wrapped in quotes, thus treating it as a string.

    Try this instead:

    {"redeliveredDataEventIndices":[]}