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

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.