Search code examples
pythontodoist

How to get API to output one parameter in JSON


I'm trying to withdraw all the tasks from a specific project within Todoist using their API and Python.

My code looks like this:

ListOfProjects = api.get_projects()

ListOfPeople = api.get_tasks(project_id = 1234567899,)

file = open('outputa.txt', 'w', encoding="utf-8")

print(ListOfPeople, file = file)

file.close()

input("Press Enter To Exit")

This then prints the JSON Encoded information to said file, for example:

[
    Task(
        id: 2995104339,
        project_id: 2203306141,
        section_id: 7025,
        parent_id: 2995104589,
        content: 'Buy Milk',
        description: '',
        comment_count: 10,
        assignee: 2671142,
        assigner: 2671362,
        order: 1,
        priority: 1,
        url: 'https://todoist.com/showTask?id=2995104339'
    ...
)

This gives me a massive, unwieldy text document (as there is one of the above for every task in a nearly 300 task project). I just need the string after the Content parameter.

Is there a way to specify that just the Content parameter should be printed?


Solution

  • According to documentation, get_tasks method returns a list of Task object, so if you need to store just content property for each of them, you can change your code like this:

    data = [t.content for t in api.get_tasks(project_id = 1234567899,)]
    with open('outputa.txt', 'w', encoding="utf-8") as f:
        print(data, file=f)