how to properly convert avro schema into a json schema
Asked Answered
M

1

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?

Meade answered 12/10, 2016 at 0:25 Comment(4)
Are you aware of github.com/fge/json-schema-avro ?Klimesh
I will take a close look. thanks!Meade
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.Lubber
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.Tightfisted
C
-4
  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.

Cannon answered 9/1, 2018 at 16:14 Comment(1)
Thats JSON not a SCHEMA!Shirty

© 2022 - 2024 — McMap. All rights reserved.