Json file to csv python

First, your JSON has nested objects, so it normally cannot be directly converted to CSV. You need to change that to something like this:

{
    "pk": 22,
    "model": "auth.permission",
    "codename": "add_logentry",
    "content_type": 8,
    "name": "Can add log entry"
},
......]

Here is my code to generate CSV from that:

import csv
import json

x = """[
    {
        "pk": 22,
        "model": "auth.permission",
        "fields": {
            "codename": "add_logentry",
            "name": "Can add log entry",
            "content_type": 8
        }
    },
    {
        "pk": 23,
        "model": "auth.permission",
        "fields": {
            "codename": "change_logentry",
            "name": "Can change log entry",
            "content_type": 8
        }
    },
    {
        "pk": 24,
        "model": "auth.permission",
        "fields": {
            "codename": "delete_logentry",
            "name": "Can delete log entry",
            "content_type": 8
        }
    }
]"""

x = json.loads[x]

f = csv.writer[open["test.csv", "wb+"]]

# Write CSV Header, If you dont need that, remove this line
f.writerow[["pk", "model", "codename", "name", "content_type"]]

for x in x:
    f.writerow[[x["pk"],
                x["model"],
                x["fields"]["codename"],
                x["fields"]["name"],
                x["fields"]["content_type"]]]

You will get output as:

pk,model,codename,name,content_type
22,auth.permission,add_logentry,Can add log entry,8
23,auth.permission,change_logentry,Can change log entry,8
24,auth.permission,delete_logentry,Can delete log entry,8

In this guide, you’ll see the steps to convert a JSON string to CSV using Python.

To begin, you may use the following template to perform the conversion:

import pandas as pd
df = pd.read_json [r'Path where the JSON file is saved\File Name.json']
df.to_csv [r'Path where the new CSV file will be stored\New File Name.csv', index = None]

In the next section, you’ll see how to apply the above template in practice.

Step 1: Prepare a JSON String

To start, prepare a JSON string that you’d like to convert to CSV.

For example, let’s say that you’d like to prepare a JSON string based on the following information about different products:

Product Price
Desktop Computer 700
Tablet 250
Printer 100
Laptop 1200

This is how the JSON string would look like for our example:

{"Product":{"0":"Desktop Computer","1":"Tablet","2":"Printer","3":"Laptop"},"Price":{"0":700,"1":250,"2":100,"3":1200}}

Step 2: Create the JSON File

Once you have your JSON string ready, save it within a JSON file.

Alternatively, you may copy the JSON string into Notepad, and then save that file with a .json extension.

For our example, save the notepad as Product_List.json. Don’t forget to add the .json extension at the end of the file name.

Step 3: Install the Pandas Package

If you haven’t already done so, install the Pandas package. You may use the following command to install the Pandas package under Windows:

pip install pandas

Step 4: Convert the JSON String to CSV using Python

You may now use the following template to assist you in converting the JSON string to CSV using Python:

import pandas as pd
df = pd.read_json [r'Path where the JSON file is saved\File Name.json']
df.to_csv [r'Path where the new CSV file will be stored\New File Name.csv', index = None]

For our example:

  • The path where the JSON file is saved is: C:\Users\Ron\Desktop\Test\Product_List.json
    • Where ‘Product_List‘ is the file name, and ‘json‘ is the file extension
  • The path where the new CSV file will be stored is: C:\Users\Ron\Desktop\Test\New_Products.csv
    • Where ‘New_Products‘ is the new file name, and ‘csv‘ is the file extension

Note that you’ll need to adjust the paths to reflect the location where the files will be stored on your computer.

Here is the complete Python code to perform the conversion to CSV for our example:

import pandas as pd
df = pd.read_json [r'C:\Users\Ron\Desktop\Test\Product_List.json']
df.to_csv [r'C:\Users\Ron\Desktop\Test\New_Products.csv', index = None]

Run the code [adjusted to your paths] and you’ll see the new CSV file at your specified location.

Once you open the file, you’ll get the data about the products:

Product Price
Desktop Computer 700
Tablet 250
Printer 100
Laptop 1200

You may also want to check the following guides for other types of file conversions:

  • Convert CSV to Excel
  • Convert Excel to CSV

How do I convert a JSON file to a csv file in Python?

Steps to Convert a JSON String to CSV using Python.
Step 1: Prepare a JSON String. To start, prepare a JSON string that you'd like to convert to CSV. ... .
Step 2: Create the JSON File. ... .
Step 3: Install the Pandas Package. ... .
Step 4: Convert the JSON String to CSV using Python..

How do I convert JSON to CSV?

Convert JSON to CSV - Here's how:.
1 Upload your JSON file. Browse your computer for a JSON document you wish to convert into a CSV file. ... .
2 Convert JSON to CSV file. Once uploaded, your JSON file will automatically start converting your data to the new format. ... .
3 Save your file or send to your email..

How do I convert JSON to Excel in Python?

Steps to Convert JSON to XLSX via Python.
Load JSON file with an instance of Workbook..
Call the Workbook.Save method..
Pass output path with XLSX extension as parameter..
Check specified path for resultant XLSX file..

How do I convert multiple JSON files to CSV?

Step 1: Load the json files with the help of pandas dataframe. Step 2 : Concatenate the dataframes into one dataframe. Step 3: Convert the concatenated dataframe into CSV file.

Chủ Đề