How to get JSON from GenericRecord without types?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to get JSON from GenericRecord without types?

kant kodali
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!
Reply | Threaded
Open this post in threaded view
|

Re: How to get JSON from GenericRecord without types?

Akshay Aggarwal

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, <[hidden email]> 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!