mtth / avsc

Avro for JavaScript :zap:
MIT License
1.27k stars 147 forks source link

How to convert decoded avro data into JSON? #437

Closed lk-1984 closed 1 year ago

lk-1984 commented 1 year ago

Hi!

I have an issue where I decode data with fromBuffer() function and it will say "Error: trailing data". Now, if I use decode() function it will not throw any error. Which one I should use?

I need to be able to write the data into disk as JSON, but if I use decode(), then it contains type definitions and I have Buffers written into the file as type:Buffer, data: "123,134,...".

If I use toJSON, it is not the data.

I spent quite lot of time with the API reference.

Note: I have completely working Python setup which means that the Schema and data itself are working. I just don't seem to get the workflow right with this library in NodeJS.

Example

"FOOBAR_ID":{"low":1010901596,"high":0,"unsigned":false}

This should be

"FOOBAR_ID": 1010901596

@mtth How I can do this after decoding?

mtth commented 1 year ago

Hi @lk-1984. type.toString will give you the data's JSON encoding:

let buf; // Your encoded data
const {value} = type.decode(buf);
const output = type.toString(value); // JSON-formatted string

fromBuffer is similar to encode but expects the buffer to only contain a single encoded value by default. The error you mention indicates that there were unused bytes in the decoded buffer.