CSV to JSON Conversion Function

  • Share this:

Code introduction


This function reads a CSV file and converts its content into JSON format, then writes the JSON data to another file.


Technology Stack : csv, json, os, re, sys, time

Code Type : Function

Code Difficulty : Intermediate


                
                    
import csv
import json
import os
import re
import sys
import time

def read_csv_to_json(csv_file_path, json_file_path):
    with open(csv_file_path, mode='r', encoding='utf-8') as csv_file:
        csv_reader = csv.DictReader(csv_file)
        data = [row for row in csv_reader]
    
    with open(json_file_path, mode='w', encoding='utf-8') as json_file:
        json.dump(data, json_file, ensure_ascii=False, indent=4)                
              
Tags: