Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save groupdocs-cloud-gists/01de2d5fce794b2470d55ca7b98b8612 to your computer and use it in GitHub Desktop.
Save groupdocs-cloud-gists/01de2d5fce794b2470d55ca7b98b8612 to your computer and use it in GitHub Desktop.
How to Convert CSV to JSON and JSON to CSV in Python

You can convert Convert CSV to JSON and JSON to CSV in Python programmatically on the cloud. In this article, we will learn how to convert CSV to JSON and JSON to CSV in Python using REST API.

The following topics shall be covered in this article:

  1. Python API for CSV to JSON and JSON to CSV Conversion
  2. How to Convert CSV to JSON in Python
  3. How to Convert JSON to CSV using Python
# How to Convert PowerPoint PPT or PPTX to JPG/JPEG format in Python
try:
# Create an instance of the API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.storage_name = storage_name
settings.file_path = "python-testing/sample-csv-file.csv"
settings.format = "json"
loadOptions = groupdocs_conversion_cloud.CsvLoadOptions()
loadOptions.separator = ","
settings.load_options = loadOptions
settings.output_path = "python-testing"
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
response = convert_api.convert_document(request)
print("Successfully converted CSV format to JSON file: " + str(response))
except groupdocs_conversion_cloud.ApiException as e:
print("Exception while calling API: {0}".format(e.message))
# API initialization to download converted file
import shutil
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# Create download json file request
request = groupdocs_conversion_cloud.DownloadFileRequest("python-testing\\sample-csv-file.json", storage_name)
# Download converted file
response = file_api.download_file(request)
# Move the downloaded json file to your local directory
shutil.move(response, "H:\\groupdocs-cloud-data\\")
# Upload CSV file to your cloud storage
# Create an instance of the File API
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# Call upload file request
request = groupdocs_conversion_cloud.UploadFileRequest("python-testing\sample-csv-file.csv", "H:\\groupdocs-cloud-data\\sample-csv-file.csv", storage_name)
# Upload json file to the cloud
response = file_api.upload_file(request)
print(response.uploaded)
# How to Convert JSON file to CSV format in Python
try:
# Create an instance of the API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.storage_name = storage_name
settings.file_path = "python-testing/sample-file.json"
settings.format = "csv"
settings.output_path = "python-testing"
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
response = convert_api.convert_document(request)
print("Successfully converted json to csv file: " + str(response))
except groupdocs_conversion_cloud.ApiException as e:
print("Exception while calling API: {0}".format(e.message))
# Import Python SDK in your python application from http://api.groupdocs.cloud
import groupdocs_conversion_cloud
# Get client_id and client_secret from https://dashboard.groupdocs.cloud after free registration.
client_id = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
client_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# Get File API configurations
configuration = groupdocs_conversion_cloud.Configuration(client_id, client_secret)
configuration.api_base_url = "https://api.groupdocs.cloud"
storage_name = "InternalStorage"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment