6

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?

Eugene Goldberg
  • 14,286
  • 20
  • 94
  • 167
  • Are you aware of https://github.com/fge/json-schema-avro ? – Chin Huang Oct 12 '16 at 16:54
  • I will take a close look. thanks! – Eugene Goldberg Oct 12 '16 at 17:01
  • 1
    Last time this was mantained was 2014. I think this will not support Json schema draft v4. Also I would not use projects not updated > 12 month. – hiaclibe Jan 12 '17 at 13:21
  • 2
    Hi Eugene, I am looking for a similar solution wherein i want to avro schema to json schema. Could you please let me know how did you manage to get this working ? I couldn't find much help from the github link provided in the comment. – Vrushank Doshi Jun 27 '17 at 03:26

1 Answers1

-2
  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.

U880D
  • 8,601
  • 6
  • 24
  • 40