Package: gbqschema-converter
Google BigQuery Table Schema ConverterPython library to convertGoogle BigQuery table schemaintodraft-07json schemaand vice versa.The library includes two main modules:gbqschema_converter
├──gbqschema_to_jsonschema.py
└──jsonschema_to_gbqschema.pyEach of those modules has two main functions:json_representation: corresponds to json output (input forgbqschema_to_jsonschema).sdk_representation: corresponds toGoogle Python SDK formatoutput (input forgbqschema_to_jsonschema).Installationpython3-mvenvenv&&source${PWD}/env/bin/activate(env)pipinstall--no-cache-dirgbqschema_converterUsage: CLIConvert json-schema to GBQ table schema(env)json2gbq-h
usage:json2gbq[-h](-iINPUT|-fFILE)GoogleBigQueryTableSchemaConverter

optionalarguments:-h,--helpshowthishelpmessageandexit-iINPUT,--inputINPUTInputobjectasstring.-fFILE,--fileFILEInputobjectasfilepath.Example: stdinExecution:(env)json2gbq-i'{"$schema": "http://json-schema.org/draft-07/schema#","type": "array","items": {"$ref": "#/definitions/element"},"definitions": {"element": {"type": "object","properties": {"att_01": {"type": "integer","description": "Att 1"},"att_02": {"type": "number","description": "Att 2"},"att_03": {"type": "string"},"att_04": {"type": "boolean"},"att_05": {"type": "string","format": "date"},"att_06": {"type": "string","format": "date-time"},"att_07": {"type": "string","format": "time"}},"required": ["att_02"]}}}'Output:2020-04-0821:42:51.700[INFO][GoogleBigQueryTableSchemaConverter]Output(5.52mselapsed):[{"description":"Att 1","name":"att_01","type":"INTEGER","mode":"NULLABLE"},{"description":"Att 2","name":"att_02","type":"NUMERIC","mode":"REQUIRED"},{"name":"att_03","type":"STRING","mode":"NULLABLE"},{"name":"att_04","type":"BOOLEAN","mode":"NULLABLE"},{"name":"att_05","type":"DATE","mode":"NULLABLE"},{"name":"att_06","type":"TIMESTAMP","mode":"NULLABLE"},{"name":"att_07","type":"STRING","mode":"NULLABLE"}]Example: fileExecution:(env)json2gbq-f${PWD}/data/jsonschema.jsonOutput:2020-04-0821:57:25.516[INFO][GoogleBigQueryTableSchemaConverter]Output(6.39mselapsed):[{"description":"Att 1","name":"att_01","type":"INTEGER","mode":"NULLABLE"},{"description":"Att 2","name":"att_02","type":"NUMERIC","mode":"REQUIRED"},{"name":"att_03","type":"STRING","mode":"NULLABLE"},{"name":"att_04","type":"BOOLEAN","mode":"NULLABLE"},{"name":"att_05","type":"DATE","mode":"NULLABLE"},{"name":"att_06","type":"TIMESTAMP","mode":"NULLABLE"},{"name":"att_07","type":"STRING","mode":"NULLABLE"}]Convert GBQ table schema to json-schema(env)gbq2json-h
usage:gbq2json[-h](-iINPUT|-fFILE)GoogleBigQueryTableSchemaConverter

optionalarguments:-h,--helpshowthishelpmessageandexit-iINPUT,--inputINPUTInputobjectasstring.-fFILE,--fileFILEInputobjectasfilepath.Example: stdinExecution:(env)gbq2json-i'[{"description": "Att 1","name": "att_01","type": "INTEGER","mode": "NULLABLE"},{"description": "Att 2","name": "att_02","type": "NUMERIC","mode": "REQUIRED"},{"name": "att_03","type": "STRING","mode": "NULLABLE"},{"name": "att_04","type": "BOOLEAN","mode": "NULLABLE"},{"name": "att_05","type": "DATE","mode": "NULLABLE"},{"name": "att_06","type": "DATETIME","mode": "NULLABLE"},{"name": "att_07","type": "TIMESTAMP","mode": "NULLABLE"}]'Output:2020-04-0821:51:05.370[INFO][GoogleBigQueryTableSchemaConverter]Output(1.08mselapsed):{"$schema":"http://json-schema.org/draft-07/schema#","type":"array","items":{"$ref":"#/definitions/element"},"definitions":{"element":{"type":"object","properties":{"att_01":{"type":"integer","description":"Att 1"},"att_02":{"type":"number","description":"Att 2"},"att_03":{"type":"string"},"att_04":{"type":"boolean"},"att_05":{"type":"string","format":"date"},"att_06":{"type":"string","pattern":"^[0-9]{4}-((|0)[1-9]|1[0-2])-((|[0-2])[1-9]|3[0-1])(|T)((|[0-1])[0-9]|2[0-3]):((|[0-5])[0-9]):((|[0-5])[0-9])(|.[0-9]{1,6})$"},"att_07":{"type":"string","format":"date-time"}},"additionalProperties":false,"required":["att_02"]}}}Example: fileExecution:(env)gbq2json-f${PWD}/data/gbqschema.jsonOutput:2020-04-0821:55:20.275[INFO][GoogleBigQueryTableSchemaConverter]Output(1.72mselapsed):{"$schema":"http://json-schema.org/draft-07/schema#","type":"array","items":{"$ref":"#/definitions/element"},"definitions":{"element":{"type":"object","properties":{"att_01":{"type":"integer","description":"Att 1"},"att_02":{"type":"number","description":"Att 2"},"att_03":{"type":"string"},"att_04":{"type":"boolean"},"att_05":{"type":"string","format":"date"},"att_06":{"type":"string","pattern":"^[0-9]{4}-((|0)[1-9]|1[0-2])-((|[0-2])[1-9]|3[0-1])(|T)((|[0-1])[0-9]|2[0-3]):((|[0-5])[0-9]):((|[0-5])[0-9])(|.[0-9]{1,6})$"},"att_07":{"type":"string","format":"date-time"}},"additionalProperties":false,"required":["att_02"]}}}Usage: python programConvert json-schema to GBQ table schemaExample: output as jsonfromgbqschema_converter.jsonschema_to_gbqschemaimportjson_representationasconverterschema_in={"$schema":"http://json-schema.org/draft-07/schema#","type":"array","items":{"$ref":"#/definitions/element",},"definitions":{"element":{"type":"object","properties":{"att_01":{"type":"integer","description":"Att 1"},"att_02":{"type":"number",},}"required":["att_02",],},},}schema_out=converter(schema_in)print(schema_out)Output:[{'description':'Att 1','name':'att_01','type':'INTEGER','mode':'NULLABLE'},{'name':'att_02','type':'NUMERIC','mode':'REQUIRED'}]Example: output as list of SchemaField (SDK format)fromgbqschema_converter.jsonschema_to_gbqschemaimportsdk_representationasconverterschema_in={"$schema":"http://json-schema.org/draft-07/schema#","type":"array","items":{"$ref":"#/definitions/element",},"definitions":{"element":{"type":"object","properties":{"att_01":{"type":"integer","description":"Att 1"},"att_02":{"type":"number",},},"required":["att_02",],},},}schema_out=converter(schema_in)print(schema_out)Output:[SchemaField('att_01','INTEGER','NULLABLE','Att 1',()),SchemaField('att_02','NUMERIC','REQUIRED',None,())]Convert GBQ table schema to json-schemaExample: output as jsonfromgbqschema_converter.gbqschema_to_jsonschemaimportjson_representationasconverterschema_in=[{'description':'Att 1','name':'att_01','type':'INTEGER','mode':'NULLABLE'},{'name':'att_02','type':'NUMERIC','mode':'REQUIRED'}]schema_out=converter(schema_in)print(schema_out)Output:{'$schema':'http://json-schema.org/draft-07/schema#','type':'array','items':{'$ref':'#/definitions/element'},'definitions':{'element':{'type':'object','properties':{'att_01':{'type':'integer','description':'Att 1'},'att_02':{'type':'number'}},'additionalProperties':False,'required':['att_02']}}}Example: output as list of SchemaField (SDK format)fromgbqschema_converter.gbqschema_to_jsonschemaimportsdk_representationasconverterfromgoogle.cloud.bigqueryimportSchemaFieldschema_in=[SchemaField('att_01','INTEGER','NULLABLE','Att 1',()),SchemaField('att_02','NUMERIC','REQUIRED',None,()),]schema_out=converter(schema_in)print(schema_out)Output:{'$schema':'http://json-schema.org/draft-07/schema#','type':'array','items':{'$ref':'#/definitions/element'},'definitions':{'element':{'type':'object','properties':{'att_01':{'type':'integer','description':'Att 1'},'att_02':{'type':'number'}},'additionalProperties':False,'required':['att_02']}}}