CSV to JSON Conversion Function

  • Share this:

Code introduction


This function reads a CSV file, converts its content to JSON format, and saves it to the specified JSON file.


Technology Stack : csv, json

Code Type : Data processing

Code Difficulty : Intermediate


                
                    
import csv
import re
import time
import datetime
import random
import json
import sys
import os

def parse_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: