Convert CSV to JSON and JSON to CSV in Python

Convert CSV to JSON and JSON to CSV in Python

CSV is a data storage format that contain comma-separated values. It is normally used to store tabular data that can also be displayed in spreadsheet applications like MS Excel. But CSV file does not support data hierarchies. JSON or JavaScript Object Notation is a light-weight structured data format type. It is also used as an alternative to XML for storing and transmitting data. So, if you need to transfer the tabular data or store the structured data into tabular form, it requires converting formats into one another. In this article, we will learn how to convert CSV to JSON and JSON to CSV in Python

The following topics are covered below:

Python API for CSV to JSON and JSON to CSV Conversion

GroupDocs.Conversion has APIs that allow the conversion of JSON and CSV files into each other. In this article, we will use the Python SDK of GroupDocs.Conversion Cloud API for converting JSON into CSV file and CSV into JSON format. It is a feature-rich, platform independent documents and images conversion Python library. It provides quick conversion of images and documents of any supported file format in high-quality like word-processing documents, spreadsheets, presentations, images, and many more.

You can install Python conversion SDK into your Python application using the following command in the console:

pip install groupdocs_conversion_cloud

Please get your Client ID and Secret from the dashboard before following the mentioned steps. Once you have your ID and secret, add code in your python application:

How to Convert CSV to JSON using Python

You can convert csv to json file by following the simple steps mentioned below:

Upload the File

Firstly, upload the CSV file to the cloud using the code example given below:

As a result, the uploaded CSV file will be available in the files section of your dashboard on the cloud.

Converting JSON file to new CSV file using Python Script

The following steps allow converting the JSON files to CSV format programmatically in the Python applications.

  • Firstly, create an instance of the ConvertApi
  • Then, create convert settings instance using ConvertSettings
  • Next, provide the files storage name
  • Set input CSV file path and output format as “json”
  • Next, create an instance of the CsvLoadOptions.
  • Provide the CSV separator
  • Then, set the load_options and output_path
  • After that, create the ConvertDocumentRequest with ConvertSettings as an argument
  • Finally, convert pdf to excel by calling the convert_document() with ConvertDocumentRequest

The following code sample shows how to change CSV to JSON format using Python:

Finally, the above code sample will save the JSON file on the cloud. This is the best way to convert csv to json file.

Download the Converted File

The above code sample will save the converted csv to json file on the cloud. You can download it using the following code sample:

How to Convert JSON to CSV using Python

The following steps allow converting the JSON file to CSV file in your Python application.

  • First, create an instance of the ConvertApi
  • Then, create convert settings instance using ConvertSettings
  • Next, provide the your cloud storage name
  • Set input JSON file path and output format as “csv”
  • Then, set the output file path
  • Now, create the ConvertDocumentRequest with ConvertSettings as an argument
  • Finally, convert pdf to excel code online by calling the convert_document() method with ConvertDocumentRequest

The following code sample shows how to convert JSON format to CSV document using Python:

Finally, the above code sample will save the CSV file on the cloud.

Online JSON and CSV Converter for Free

Groupdocs.Conversion provides free online CSV to JSON converter and online JSON to CSV converter free. It has been developed using the Groupdocs.Conversion Cloud APIs.

Conclusion

To conclude, you learned how to convert the JSON files and CSV format and also the conversion of CSV files to JSON format programmatically . Now you understand:

  • how to convert CSV to JSON programmatically;
  • how to convert JSON to CSV programmatically;

In addition, you can learn more about GroupDocs.Conversion file format conversion API using the documentation, or by examples available on GitHub. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser.

Ask a question

You can ask your queries about how to convert JSON to CSV or CSV to JSON format, via our Free Support Forum

See Also