Playbooks

Python Script to Convert JSON to CSV

Here is an example Python script that converts a JSON file to a CSV file:

import json
import csv

# Open the JSON file
with open('data.json', 'r') as json_file:
    json_data = json.load(json_file)

# Open a CSV file for writing
with open('data.csv', 'w', newline='') as csv_file:
    # Create a CSV writer
    csv_writer = csv.writer(csv_file)
    
    # Write the headers
    csv_writer.writerow(json_data[0].keys())

    # Write the data
    for row in json_data:
        csv_writer.writerow(row.values())

This script assumes that the JSON file is an array of objects, where each object represents a row in the CSV file. It will write the keys of the first object to the headers of the csv file. It will then write the values of the each object to the csv file. It will use json library to load the json data and csv library to write the data to csv file You can customize this script to suit the specific format of your JSON data and the desired structure of your CSV file.

PyJoy

Recent Posts

Python Script to Monitor Network Traffic

Here is a simple Python script to monitor network traffic. import psutil def monitor_network(): #…

2 years ago

Python Script for Web Scraping

Here's a basic example of web scraping in Python using the requests and BeautifulSoup libraries:…

2 years ago

Python Script to SSH and Run Commands

Here's an example of how you can use Python to SSH into a remote server…

2 years ago

Python Code Example with Classes

Here's an example of a Python class that represents a rectangle: class Rectangle: def __init__(self,…

2 years ago

Python Script Example with Main Method in Active Directory

Here is a simple Python script that interacts with Active Directory (AD) using the pyad…

2 years ago

Python Script Example for System Administrators

Here's a Python script to monitor disk usage on a system and send an email…

2 years ago