convert json schema to avro schema in python
Asked Answered
H

0

8

I want to convert json schema to avro schema using python because I'm building my microservice in Python Fastapi.

json-schema

{
   "type":"object",
   "properties":{
      "IsNonPO":{
         "title":"IsNonPO",
         "type":[
            "boolean",
            "null"
         ],
         "precision":null,
         "scale":null,
         "size":null,
         "allowedValues":null
      },
      "ApprovedState":{
         "title":"ApprovedState",
         "type":[
            "number",
            "null"
         ],
         "precision":null,
         "scale":null,
         "size":null,
         "allowedValues":[
            {
               "key":"8",
               "value":"Invalid"
            },
            {
               "key":"1",
               "value":"Composing"
            },
            {
               "key":"2",
               "value":"Submitted"
            },
            {
               "key":"4",
               "value":"Approved"
            },
            {
               "key":"16",
               "value":"Denied"
            }
         ]
      },
      "CreateDate":{
         "title":"CreateDate",
         "type":[
            "string",
            "null"
         ],
         "precision":null,
         "scale":null,
         "size":null,
         "allowedValues":null,
         "format":"date-time"
      },
      "RemitToAddress": {
            "type": ["object", "null"],
            "properties": {
                "State": {
                    "title": "RemitToAddress.State",
                    "type": ["string", "null"],
                    "precision": null,
                    "scale": null,
                    "size": 50,
                    "allowedValues": null
                },
                "Phone": {
                    "title": "RemitToAddress.Phone",
                    "type": ["string", "null"],
                    "precision": null,
                    "scale": null,
                    "size": 70,
                    "allowedValues": null
                },
                "Country": {
                    "type": ["object", "null"],
                    "properties": {
                        "UniqueName": {
                            "title": "RemitToAddress.Country.UniqueName",
                            "type": ["string", "null"],
                            "precision": null,
                            "scale": null,
                            "size": 50,
                            "allowedValues": null
                        }
                    }
                },
                "PostalCode": {
                    "title": "RemitToAddress.PostalCode",
                    "type": ["string", "null"],
                    "precision": null,
                    "scale": null,
                    "size": 50,
                    "allowedValues": null
                },
                "City": {
                    "title": "RemitToAddress.City",
                    "type": ["string", "null"],
                    "precision": null,
                    "scale": null,
                    "size": 50,
                    "allowedValues": null
                },
                "Fax": {
                    "title": "RemitToAddress.Fax",
                    "type": ["string", "null"],
                    "precision": null,
                    "scale": null,
                    "size": 70,
                    "allowedValues": null
                },
                "UniqueName": {
                    "title": "RemitToAddress.UniqueName",
                    "type": ["string", "null"],
                    "precision": null,
                    "scale": null,
                    "size": 50,
                    "allowedValues": null
                },
                "Lines": {
                    "title": "RemitToAddress.Lines",
                    "type": ["string", "null"],
                    "precision": null,
                    "scale": null,
                    "size": 1024,
                    "allowedValues": null
                },
                "Name": {
                    "title": "RemitToAddress.Name",
                    "type": ["string", "null"],
                    "precision": null,
                    "scale": null,
                    "size": 128,
                    "allowedValues": null
                }
            }
        }
    }
}

Avro schema

{
   "type":"record",
   "name":"invoice",
   "namespace":"com.xyz.com",
   "fields":[
      {
         "name":"IsNonPO",
         "type":[
            "null",
            "boolean"
         ]
      },
      {
         "name":"ApprovedState",
         "type":[
            "null",
            "long"
         ]
      },
      {
         "name":"CreateDate",
         "type":[
            "null",
            {
               "type":"string",
               "logicalType":"timestamp-micros"
            }
         ]
      },
      {
         "name":"RemitToAddress",
         "type":[
            {
               "type":"record",
               "name":"RemitToAddress",
               "namespace":"com.xyz.com.invoice",
               "fields":[
                  {
                     "name":"City",
                     "type":[
                        "null",
                        "string"
                     ]
                  },
                  {
                     "name":"Country",
                     "type":[
                        {
                           "type":"record",
                           "name":"Country",
                           "namespace":"com.xyz.com.invoice.RemitToAddress",
                           "fields":[
                              {
                                 "name":"UniqueName",
                                 "type":[
                                    "null",
                                    "string"
                                 ]
                              }
                           ]
                        },
                        "null"
                     ]
                  },
                  {
                     "name":"Fax",
                     "type":[
                        "null",
                        "string"
                     ]
                  },
                  {
                     "name":"Lines",
                     "type":[
                        "null",
                        "string"
                     ]
                  },
                  {
                     "name":"Name",
                     "type":[
                        "null",
                        "string"
                     ]
                  },
                  {
                     "name":"Phone",
                     "type":[
                        "null",
                        "string"
                     ]
                  },
                  {
                     "name":"PostalCode",
                     "type":[
                        "null",
                        "string"
                     ]
                  },
                  {
                     "name":"State",
                     "type":[
                        "null",
                        "string"
                     ]
                  },
                  {
                     "name":"UniqueName",
                     "type":[
                        "null",
                        "string"
                     ]
                  }
               ]
            },
            "null"
         ]
      }
   ]
}

I tried to find the converter in python but I can't. I found a converter but does it in java. Please let me know if there are any Python converter exists or do I write of my own library?

Hooey answered 30/9, 2020 at 4:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.