how to convert json to csv python code example

Example 1: python csv string to json

import json
import csv 
//string_data is your string in csv format
json.dumps(list(csv.DictReader(string_data.splitlines(), delimiter=",")))

Example 2: convert response to json python

#You can use json.loads:

import json
import requests

response = requests.get(...)
json_data = json.loads(response.text)

Example 3: json to csv python

import pandas as pd
df = pd.read_json (r'C:\Users\Ron\Desktop\Test\Product_List.json')
export_csv = df.to_csv (r'C:\Users\Ron\Desktop\Test\New_Products.csv', index = None, header=True)

Example 4: python json from csv

import csv
import json

csvfile = open('file.csv', 'r')
jsonfile = open('file.json', 'w')

fieldnames = ("FirstName","LastName","IDNumber","Message")
reader = csv.DictReader( csvfile, fieldnames)
for row in reader:
    json.dump(row, jsonfile)
    jsonfile.write('\n')

Example 5: append to csv python

with open('document.csv','a') as fd:
    fd.write(myCsvRow)

Example 6: convert csv to json python

import pandas as pd
df = pd.read_csv (r'Path where the CSV file is saved\File Name.csv')
df.to_json (r'Path where the new JSON file will be stored\New File Name.json')