pythonjsonasana-api

How to convert Asana API response with multiple JSON objects to Python objects


I'm new to Python. I'm working on a script to send notifications on overdue Asana tasks. I'm running into issues with converting the Asana API response, which is a JSON with multiple objects that represent tasks, to Python objects. For now, all I want to do is convert the JSON objects into Python objects to validate the response.

This is what the raw JSON response from Asana looks like:

{
    "data": [
      {
        "gid": "1234567891234567",
        "due_on": "2021-02-12",
        "name": "My First Task",
        "permalink_url": "https://app.asana.com/0/123456789/1234567891234567"
      },
      {
        "gid": "1234567891234568",
        "due_on": "2021-02-26",
        "name": "My Second Task",
        "permalink_url": "https://app.asana.com/0/123456789/1234567891234568"
      },
      {
        "gid": "1234567891234569",
        "due_on": null,
        "name": "My Third Task",
        "permalink_url": "https://app.asana.com/0/123456789/1234567891234569"
      }
    ]
  }

My Python code looks like the following:

import json
import asana


taskList = []
with open('./work_in_progress.json') as f:
    for jsonObj in f:
        taskDict = json.loads(jsonObj)
        taskList.append(taskDict)

print("Printing each JSON Decoded Object")
for task in taskList:
    print(task["gid"], task["name"], task["due_on"], task["permalink"])

However, when I run it, I get the following error:

Traceback (most recent call last):
  File "test.py", line 9, in <module>
    taskDict = json.loads(jsonObj)
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 380, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting object: line 1 column 2 (char 1)

Any help would be appreciated.


Solution

  • Have you tried the following? Replace the with statement with this code:

    with open('./work_in_progress.json') as f:
        taskDict = json.load(f)
        taskList.append(taskDict)