You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by kant kodali <ka...@gmail.com> on 2017/07/16 01:36:38 UTC

How to get JSON from GenericRecord without types?

Hi All,

I have the following code.

public String byte[] toJson(Schema schema, GenericRecord genericRecord) throws
IOException {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DatumWriter<GenericRecord> writer = new GenericDatumWriter<>(schema);
    JsonEncoder encoder = EncoderFactory.get().jsonEncoder(schema, baos);
    writer.write(genericRecord, encoder);
    encoder.flush();
    baos.flush();
    return new String(baos.toByteArray());

}


As you can see I am passing a Generic Record and I just need a String

without the types.

Right now The function above generates something like this

{"NUM_7":{"double":0.0},"NUM_8":{"double":0.0}}
What I need is this

{"NUM_7":0.0},"NUM_8":0.0}


Thanks!

Re: How to get JSON from GenericRecord without types?

Posted by Akshay Aggarwal <ak...@flipkart.com>.
Hi Kant, The toString method of GenericRecord returns a JSON payload
without the type information.

Cheers!
Akshay

On Sun, 16 Jul 2017, 07:07 kant kodali, <ka...@gmail.com> wrote:

> Hi All,
>
> I have the following code.
>
> public String byte[] toJson(Schema schema, GenericRecord genericRecord) throws
> IOException {
>
>     ByteArrayOutputStream baos = new ByteArrayOutputStream();
>     DatumWriter<GenericRecord> writer = new GenericDatumWriter<>(schema);
>     JsonEncoder encoder = EncoderFactory.get().jsonEncoder(schema, baos);
>     writer.write(genericRecord, encoder);
>     encoder.flush();
>     baos.flush();
>     return new String(baos.toByteArray());
>
> }
>
>
> As you can see I am passing a Generic Record and I just need a String
>
> without the types.
>
> Right now The function above generates something like this
>
> {"NUM_7":{"double":0.0},"NUM_8":{"double":0.0}}
> What I need is this
>
> {"NUM_7":0.0},"NUM_8":0.0}
>
>
> Thanks!
>
>