Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
    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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading