title: Converting Avro Binary String to Json
body: <p>I have a String in Avro Binary Format. I want to convert the String to json. Can someone please guide me ?. I tried using the solutions available online It did not work.</p>
  
  <pre><code>public String avroToJson(byte[] avro) throws IOException {
          boolean pretty = false;
          GenericDatumReader&lt;GenericRecord&gt; reader = null;
          JsonEncoder encoder = null;
          ByteArrayOutputStream output = null;
          try {
              reader = new GenericDatumReader&lt;GenericRecord&gt;();
              InputStream input = new ByteArrayInputStream(avro);
              DataFileStream&lt;GenericRecord&gt; streamReader = new DataFileStream&lt;GenericRecord&gt;(input, reader);
              output = new ByteArrayOutputStream();
              Schema schema = streamReader.getSchema();
              DatumWriter&lt;GenericRecord&gt; writer = new GenericDatumWriter&lt;GenericRecord&gt;(schema);
              encoder = EncoderFactory.get().jsonEncoder(schema, output, pretty);
              for (GenericRecord datum : streamReader) {
                  writer.write(datum, encoder);
              }
              encoder.flush();
              output.flush();
              return new String(output.toByteArray());
          } finally {
              try {
                  if (output != null) output.close();
              } catch (Exception e) {
              }
          }
      }
  </code></pre>
  
  <p>I am converting the my String to byte array using getBytes() and passing it to this function. I am getting this exception. 
  Exception in thread "main" org.apache.avro.InvalidAvroMagicException: Not an Avro data file.</p>