import csv to json code example
Example 1: 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 2: 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')
Example 3: convert json to csv npm
const { Parser } = require('json2csv'); const fields = ['field1', 'field2', 'field3'];const opts = { fields }; try { const parser = new Parser(opts); const csv = parser.parse(myData); console.log(csv);} catch (err) { console.error(err);}