You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by "Gregory (Grisha) Trubetskoy" <gr...@apache.org> on 2013/06/19 23:34:29 UTC

json2avro

I wrote a little C tool using Avro-C to convert JSON to Avro and thought 
may be someone here may find it useful.

https://github.com/grisha/json2avro

The purpose is to be useful in converting messy legacy JSON in which 
some elements might be missing or of wrong type. Even though there is no 
schema resolution per se here, json2avro will attempt to use the default 
specified in the schema if the corresponding JSON element is missing and 
will attempt to try the types specified in a union until one succeeds.

json2avro lets you pick from null, snappy, deflate and lzma codecs, 
specify a custom block size and optionally skips over JSON lines that it 
is unable to parse. I'm also thinking of adding a target max file size so 
that it would automatically split output into multiple sizes.

It uses Jansson as the JSON parser which is conveniently bundled with 
Avro-C. (One thing that I'm not clear on is that Jansson cannot handle 
nulls, not sure if this is a Jansson-specific limitation or something 
inherent to JSON.)

This is rather simple code (no tests, and not even a "make install" yet) 
and lacks support for some features, namely enums and aliases, but it's 
good enough to be useful. It does seem pretty fast, slightly faster than 
the avro-tools fromjson option (though my tests were hardly scientific).

Enjoy, and any feedback is very much appreciated!

Grisha

Re: json2avro

Posted by Douglas Creager <do...@creagertino.net>.
> Sorry I wasn't clear. Jansson uses null-terminated strings. The docs
> state "Normal null terminated C strings are used, so JSON strings may
> not contain embedded null characters." I've tested it and indeed, they
> cannot, Jansson cannot parse a string like "abc\u0000def".

Oh yes, NUL bytes in a string, gotcha.  You're right, that's my least
favorite thing about Jansson.  There's an open issue with a patch [1],
but there hasn't been any movement on it in a year, unfortunately.

[1] https://github.com/akheron/jansson/pull/63



Re: json2avro

Posted by "Gregory (Grisha) Trubetskoy" <gr...@apache.org>.
On Tue, 25 Jun 2013, Douglas Creager wrote:

>> json2avro lets you pick from null, snappy, deflate and lzma codecs,
>> specify a custom block size and optionally skips over JSON lines that it
>> is unable to parse. I'm also thinking of adding a target max file size
>> so that it would automatically split output into multiple sizes.
>
> Very cool!  Kind of the reverse of avrocat or avropipe.  We could clean
> it up and add it as another C command-line tool if you like.

Sure I'm all for it!

>> It uses Jansson as the JSON parser which is conveniently bundled with
>> Avro-C. (One thing that I'm not clear on is that Jansson cannot handle
>> nulls, not sure if this is a Jansson-specific limitation or something
>> inherent to JSON.)
>
> Can you elaborate on this?  Jansson should support null JSON values
> (it's the keyword null, not the string value "null").  And the Avro C
> bindings should use that for Avro null values.

Sorry I wasn't clear. Jansson uses null-terminated strings. The docs state 
"Normal null terminated C strings are used, so JSON strings may not 
contain embedded null characters." I've tested it and indeed, they cannot, 
Jansson cannot parse a string like "abc\u0000def".

Grisha

Re: json2avro

Posted by Douglas Creager <do...@creagertino.net>.
> I wrote a little C tool using Avro-C to convert JSON to Avro and thought
> may be someone here may find it useful.
> 
> https://github.com/grisha/json2avro
> 
> The purpose is to be useful in converting messy legacy JSON in which
> some elements might be missing or of wrong type. Even though there is no
> schema resolution per se here, json2avro will attempt to use the default
> specified in the schema if the corresponding JSON element is missing and
> will attempt to try the types specified in a union until one succeeds.
> 
> json2avro lets you pick from null, snappy, deflate and lzma codecs,
> specify a custom block size and optionally skips over JSON lines that it
> is unable to parse. I'm also thinking of adding a target max file size
> so that it would automatically split output into multiple sizes.

Very cool!  Kind of the reverse of avrocat or avropipe.  We could clean
it up and add it as another C command-line tool if you like.

> It uses Jansson as the JSON parser which is conveniently bundled with
> Avro-C. (One thing that I'm not clear on is that Jansson cannot handle
> nulls, not sure if this is a Jansson-specific limitation or something
> inherent to JSON.)

Can you elaborate on this?  Jansson should support null JSON values
(it's the keyword null, not the string value "null").  And the Avro C
bindings should use that for Avro null values.

cheers
–doug