Learn to Export Resource Data to CSV and JSON Formats with Code

Hello, techies!

Let's face it. At some point in your programming journey, whilst working with internet data resources or databases, you've wanted to export some data to a file in a format that is widely reusable. This article teaches you exactly how to do that in the shortest way possible, in one of the most widely acceptable languages out there (Python).

To be clear, the data can be from any source, but the sampled codes below were used to export data from a free and reliable API for testing and prototyping, JSONPlaceholder using Python scripts.

Exporting to CSV

For the purpose of avoiding unnecessary ambiguity, parts of the code without any connection to this topic were replaced with ". . .".

#!/usr/bin/python3
'''
    For a given employee ID (standard output), this script exports
    employee data in CSV format to a file, "<USER_ID>.csv".
'''

if __name__ == "__main__":
    import csv
    import requests
    import sys

    ID = int(sys.argv[1])
    name_URL = 'https://jsonplaceholder.typicode.com/users' + '/' + str(ID)
    TODOs_URL = 'https://jsonplaceholder.typicode.com/todos'

    r = requests.get(name_URL)
    user_dict = r.json()

    . . .

    params = {
        "userId": ID
    }
    r = requests.get(TODOs_URL, params=params)
    tasks = r.json()

    . . .

    '''Export happens here'''
    file_name = str(ID) + '.csv'
    with open(file_name, mode='w', newline='') as file:
        writer = csv.writer(file, quoting=csv.QUOTE_ALL)

        for task in tasks:
            data = [
                str(user_dict["id"]),
                str(user_dict["username"]),
                str(task["completed"]),
                str(task["title"])
            ]

            writer.writerow(data)

Exporting to JSON

For the purpose of avoiding unnecessary ambiguity, parts of the code without any connection to this topic were replaced with ". . .".

#!/usr/bin/python3
'''
    For a given employee ID (standard output), this script exports
    employee data in JSON format to a file, "<USER_ID>.json".
'''

if __name__ == "__main__":
    import json
    import requests
    import sys

    ID = int(sys.argv[1])
    name_URL = 'https://jsonplaceholder.typicode.com/users' + '/' + str(ID)
    TODOs_URL = 'https://jsonplaceholder.typicode.com/todos'

    r = requests.get(name_URL)
    user_dict = r.json()

    . . .

    params = {
        "userId": ID
    }
    r = requests.get(TODOs_URL, params=params)
    tasks = r.json()

    . . .

    '''Export happens here'''
    file_name = str(ID) + '.json'
    with open(file_name, mode='w') as file:
        data = {
            str(ID): [
                {
                    "task": str(task["title"]),
                    "completed": task["completed"],
                    "username": str(user_dict["username"])
                } for task in tasks
            ]
        }

        json.dump(data, file)

Conclusion

The "data" variables in the codes above refer to dictionaries already present in an online resource accessed via the JSONPlaceholder REST API.

Not a techie? Start your journey with me! My articles are arranged in series, arranged in the order in which you should learn them. Also, each article is explained as simply and concise as it can be. It is not a replacement for courses or other online resources but is a good accessory to these.

The recommended article you should begin with is Shell Basics.

I hope you found this content as enjoyable to read as I did to write. If you are interested in more content like this, please navigate to my profile and follow my social media channels. There, I document most of what I am learning and the projects I am currently working on.

10
Subscribe to my newsletter

Read articles from Ikechukwu Ashimonye directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ikechukwu Ashimonye
Ikechukwu Ashimonye

About Me Welcome to my developer's corner on Hashnode! I'm an Electrical and Electronics Engineering graduate with a passion for technology, creative design, and software development. My journey into the tech world began with a deep curiosity for how things work, which eventually led me to explore various facets of engineering and software. Work Experience I have a solid foundation in electrical and electronics engineering, and over the past 1-2 years, I've honed my skills in Photoshop, focusing on photo editing, minimalistic logo creation, and other creative services. My work under the brand TechGecko (TGPhotoshop) has allowed me to combine technical expertise with creativity, providing unique solutions to my clients. On the development side, I am currently building a web application called DayMinder, designed to help users manage their schedules, complete tasks, and receive timely reminders. This project reflects my interest in creating practical tools that make everyday life easier. Accomplishments Freelance Photoshop Editor: Successfully launched a freelance business on Freelancer.com and Upwork, where I offer a range of photo editing and design services. University Graduate: Completed a degree in Electrical and Electronics Engineering, gaining a strong technical background. Ongoing Web App Development: Actively working on DayMinder, a personal productivity tool, which showcases my skills in both design and software development. Interests and Dreams Creative Design: I enjoy exploring the intersection of art and technology, particularly in photo editing and design. My deep curiosity might again lead me to explore other facets of art. Who knows? Software Development: My current focus is on improving my coding skills, particularly in web and application development, and expanding my knowledge in areas like SEO and user experience design. Dreams: My goal is to continue growing as a developer and designer, eventually creating tools and products that have a meaningful impact. I also aim to establish a strong presence in the tech community, sharing my experiences and learning from others. Connect with Me Feel free to reach out on X (formerly known as Twitter) or LinkedIn via my blog home or profile page if you're interested in collaborating on projects or just want to chat about tech, design, or anything in-between. Let's create something amazing together!