Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"name": "Alice",
"age": 25,
"email": "alice@example.com"
},
{
"name": "Bob",
"age": 30,
"email": "bob@example.com"
},
{
"name": "Charlie",
"age": 35,
"email": "charlie@example.com"
}
]

28 changes: 28 additions & 0 deletions source_json_to_csv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import json
import csv

def extract_json_to_csv(json_file_path, csv_file_path):
# Open the JSON file for reading
with open(json_file_path, 'r') as json_file:
# Load the JSON data
json_data = json.load(json_file)

# Open the CSV file for writing
with open(csv_file_path, 'w', newline='') as csv_file:
# Create a CSV writer object
csv_writer = csv.writer(csv_file)

# Extract the header row from the JSON data
header = list(json_data[0].keys())
csv_writer.writerow(header)

# Extract the data rows from the JSON data
for row in json_data:
csv_writer.writerow(list(row.values()))

print(f'Successfully extracted JSON data to {csv_file_path}!')

# Example usage
json_file_path = 'example.json'
csv_file_path = 'example.csv'
extract_json_to_csv(json_file_path, csv_file_path)