how to properly convert avro schema into a json schema

15.7k Views Asked by At

I have the following json data object:

{
    "name": "John",
    "favorite_number": 5,
    "favorite_color" : "green"
}

The JSON schema for this object looks like this:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Person",
    "description": "some description",
    "type": "object",
    "properties": {
        "name": {
            "description": "name",
            "type": "string"
        },
        "favorite_number": {
            "type": "number",
        },
        "favorite_color": {
            "type": "string",
        },
    },
    "required": ["name", "favorite_number","favorite_color"]
}

I'm able to use this JSON schema, to validate whether my data object conforms to it:

public static boolean isJsonValid(String schemaText, String jsonText) throws ProcessingException, IOException
    {   
        final JsonSchema schemaNode = getSchemaNode(schemaText);
        final JsonNode jsonNode = getJsonNode(jsonText);
        return isJsonValid(schemaNode, jsonNode);
    } 

In my java application, I'm receiving a corresponding AVRO schema for this object from a REST API call, and that schema looks like this:

{
 "namespace": "example.avro",
 "type": "record",
 "name": "Person",
 "fields": [
     {"name": "name", "type": "string"},
     {"name": "favorite_number",  "type": ["int", "null"]},
     {"name": "favorite_color", "type": ["string", "null"]}
 ]
}

Is there a commonly acceptable way of converting such AVRO schema into a JSON schema?

1

There are 1 best solutions below

1
On
  1. Download: avro-tools-1.7.4.jar (or latest version from repository)
  2. Run: java -jar avro-tools-1.7.4.jar tojson avro-filename.avro>output-filename.json

This will create output-filename.json file with all the data. If output-filename.json already exists it will override it.