Python Examples

Here are some examples of how to use the Konbert API with Python. These examples demonstrate different conversion scenarios and options.

Convert JSON file to PostgreSQL

This example shows how to convert a JSON file to PostgreSQL format using the Konbert API.

import requests
import json
API_KEY = "<your_api_key>"
API_ENDPOINT = "https://konbert.com/api/v1/convert"
input_file_path = "<input_file_path>"
output_file_path = "<output_file_path>"
with open(input_file_path, 'rb') as file:
files = {'input[file]': file}
data = {
'input[format]': 'json',
'input[options][flatten_objects]': 'true',
'output[format]': 'sql',
'output[options][create_table]': 'true',
'output[options][syntax]': 'postgres',
'output[options][table_name]': 'mytable',
'sync': 'true'
}
headers = {
'Authorization': f'Bearer {API_KEY}'
}
response = requests.post(API_ENDPOINT, files=files, data=data, headers=headers)
if response.status_code == 200:
with open(output_file_path, 'wb') as output_file:
output_file.write(response.content)
print("File converted successfully")
else:
print(f"Failed to convert file: {response.text}")

Convert CSV URL to Parquet

This example demonstrates how to convert a CSV file from a URL to Parquet format.

import requests
import json
API_KEY = "<your_api_key>"
API_ENDPOINT = "https://konbert.com/api/v1/convert"
output_file_path = "<output_file_path>"
payload = {
"input": {
"http": {
"url": "https://example.com/data.csv"
},
"format": "csv",
"options": {
"delimiter": ","
}
},
"output": {
"format": "parquet"
},
"sync": True
}
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
response = requests.post(API_ENDPOINT, json=payload, headers=headers)
if response.status_code == 200:
with open(output_file_path, 'wb') as output_file:
output_file.write(response.content)
print("File converted successfully")
else:
print(f"Failed to convert file: {response.text}")