Fetch data from a specific column of a board using python

I have the following code in Python->
import requests
import json

apiKey = “my api key”
apiUrl = “https://api.monday.com/v2”
headers = {“Authorization” : apiKey}

query2 = ‘{boards (ids: 2257165976) { name id description items { name column_values{title id type text } } } }’
data = {‘query’ : query2}

r = requests.post(url=apiUrl, json=data, headers=headers) # make request
print(r.json())

This code displays everything (all the items) from a specific board but doesn’t always return the values of a column. How could I fix that? Also, I don’t want to fetch data from every column, I want to fetch data from a specific column. Could someone please modify my code in order to do so?

dude trust me, do pip install monday in the terminal
then do from monday import MondayClient

this is going to save you so much time

GitHub - ProdPerfect/monday: Python client for Monday.com documentation here

What do you mean it doesn’t return all the values of a column? Can you show the example?

You can use the library I shared above to get the column id (or through enabling dev mode and getting the col_id), then use a different function with the column id to fetch from that specific function

Here’s a snippet

client = MondayClient(apiKey)
ret = client .boards.fetch_columns_by_board_id(board_id) # you also have to know the board id which is in the url 
##Parse ret['data']['boards'][0]['columns'] for the column id
 ret=mPT.boards.fetch_items_by_board_id(1684237949)
#now parse ret using the column id for the col values you are looking for, you can use list comprehension here  for example 
 arr = client.boards.fetch_items_by_board_id(board)
 names = [item['name'] for item in [item for item in arr['data']['boards'][0]["items"]]]
#will give you the names of the items, play with it a bit
1 Like

This works alright, but when I try to pull specific column values (especially numbers) things don’t seem to work.

For example:

arr = client.boards.fetch_items_by_board_id(2885491262)
names = [item['name'] for item in [item for item in arr['data']['boards'][0]["items"]]]
print(names)
# will give you the names of the items, play with it a bit

hours = [item['numbers'] for item in [item for item in arr['data']['boards'][0]["items"][0]['column_values'][0][
    'value']]]
print(hours)

The return is:
[‘Item 1’, ‘Item 2’, ‘Item 3’, ‘Item 4’, ‘Item 5’]

Traceback (most recent call last):
hours = [item[‘numbers’] for item in [item for item in arr[‘data’][‘boards’][0][“items”][0][‘column_values’][0][
TypeError: ‘NoneType’ object is not iterable

For what it’s worth I get similar results when not using the monday module.

hours_sum = 0
column_length = len(result['data']['boards'][0]['items'][0]['column_values'])
for item in range(len(result['data']['boards'][0]['items'])):
    # print(f"Item {item}: {result['data']['boards'][0]['items'][item]['column_values']}")

    for i in range(column_length):
        if result['data']['boards'][0]['items'][0]['column_values'][i]['id'] in ['numbers', 'item']:
            line_result = int(0 if result['data']['boards'][0]['items'][0]['column_values'][i]['value'] is None else
                              result['data']['boards'][0]['items'][0]['column_values'][i]['value'])
            hours_sum += line_result
            print(item, i)
            print(result['data']['boards'][0]['items'][0]['column_values'][i])
print(f'Total Hours Spent: {hours_sum}')

line 56, in
line_result = int(0 if result[‘data’][‘boards’][0][‘items’][0][‘column_values’][i][‘value’] is None else
ValueError: invalid literal for int() with base 10: ‘“1”’

Seems like the number columns return strings and I am having trouble converting them to int.

Is anyone using python with the Monday API to really leverage their workflow? I would love to see what others are doing to make this effective.

Without acess to your specific board it’s hard to figure out what’s wrong here but generally you’re probably unpacking the json wrong somehow. Try printing out the raw api response and see how to properly unpack the data you’re trying to get.

Yes, this API feels like an exercise in unpacking JSON. Clearly I am not yet up to the task.

query_columns = """
                query {
                    boards (ids: 2885491262) {
                        items () {
                            id
                            name
                            
                        column_values {
                            id
                            title
                            value
                            text
                            additional_info   
                            }
                        }
                    }
                }
                """
data = {'query': query_columns}

r = requests.post(url=apiUrl, json=data, headers=headers)  # make request

result = r.json()

print(result)

And the output is:

{'data': {'boards': [{'items': [{'id': '2885491305', 'name': 'Item 1', 'column_values': [{'id': 'person', 'title': 'Person', 'value': None, 'text': '', 'additional_info': None}, {'id': 'status', 'title': 'Status', 'value': '{"index":0,"post_id":null,"changed_at":"2019-03-01T17:24:57.321Z"}', 'text': 'Working on it', 'additional_info': '{"label":"Working on it","color":"#fdab3d","changed_at":"2019-03-01T17:24:57.321Z"}'}, {'id': 'date4', 'title': 'Date', 'value': '{"date":"2022-07-03","icon":null,"changed_at":"2022-07-01T13:29:43.829Z"}', 'text': '2022-07-03', 'additional_info': None}, {'id': 'numbers', 'title': 'Hours', 'value': '"1"', 'text': '1', 'additional_info': None}]}, {'id': '2885491315', 'name': 'Item 2', 'column_values': [{'id': 'person', 'title': 'Person', 'value': None, 'text': '', 'additional_info': None}, {'id': 'status', 'title': 'Status', 'value': '{"index":1,"post_id":null,"changed_at":"2019-03-01T17:28:23.178Z"}', 'text': 'Done', 'additional_info': '{"label":"Done","color":"#00c875","changed_at":"2019-03-01T17:28:23.178Z"}'}, {'id': 'date4', 'title': 'Date', 'value': '{"date":"2022-07-01","icon":null,"changed_at":"2022-07-01T13:29:43.426Z"}', 'text': '2022-07-01', 'additional_info': None}, {'id': 'numbers', 'title': 'Hours', 'value': '"2"', 'text': '2', 'additional_info': None}]}, {'id': '2885491325', 'name': 'Item 3', 'column_values': [{'id': 'person', 'title': 'Person', 'value': None, 'text': '', 'additional_info': None}, {'id': 'status', 'title': 'Status', 'value': '{"index":5,"post_id":null,"changed_at":"2019-03-01T17:25:02.248Z"}', 'text': None, 'additional_info': '{"label":null,"color":"#c4c4c4","changed_at":"2019-03-01T17:25:02.248Z"}'}, {'id': 'date4', 'title': 'Date', 'value': '{"date":"2022-07-01","icon":null,"changed_at":"2022-07-01T13:29:42.918Z"}', 'text': '2022-07-01', 'additional_info': None}, {'id': 'numbers', 'title': 'Hours', 'value': '"3"', 'text': '3', 'additional_info': None}]}, {'id': '2885491341', 'name': 'Item 4', 'column_values': [{'id': 'person', 'title': 'Person', 'value': None, 'text': '', 'additional_info': None}, {'id': 'status', 'title': 'Status', 'value': None, 'text': None, 'additional_info': None}, {'id': 'date4', 'title': 'Date', 'value': '{"date":"2022-07-01","icon":null,"changed_at":"2022-07-01T13:29:42.918Z"}', 'text': '2022-07-01', 'additional_info': None}, {'id': 'numbers', 'title': 'Hours', 'value': '"4"', 'text': '4', 'additional_info': None}]}, {'id': '2885491355', 'name': 'Item 5', 'column_values': [{'id': 'person', 'title': 'Person', 'value': None, 'text': '', 'additional_info': None}, {'id': 'status', 'title': 'Status', 'value': None, 'text': None, 'additional_info': None}, {'id': 'date4', 'title': 'Date', 'value': '{"date":"2022-07-01","icon":null,"changed_at":"2022-07-01T13:29:43.426Z"}', 'text': '2022-07-01', 'additional_info': None}, {'id': 'numbers', 'title': 'Hours', 'value': '"5"', 'text': '5', 'additional_info': None}]}]}]}, 'account_id': 9792846}

It seems very odd that my numbers column is returning ‘“1”’.