I am having trouble creating items. I have read through the api documentation and have tried setting up my post request exactly shown in the docs. I unfortunately cannot share my code one for one since some of the information is sensitive. Though the problem seems to be coming from how the “column_values” are formatted.
Code
The purpose of the update_payload variable is to fix the column values. I created some code that parses data and correctly formats the forward slashes to how create_items mutation example looks like.
# python 3.10
import json
import requests
apiKey = 'xxx'
apiUrl = "https://api.monday.com/v2"
headers = {"Authorization" : apiKey,
"Content-Type" : 'application/json',
"API-Version" : '2023-10'}
def create_item(board_id, group_id, item_name, column_values):
payload = f"""
mutation {{
create_item (board_id: {board_id} group_id: {json.dumps(group_id)} item_name: {json.dumps(item_name)} column_values: {json.dumps(column_values)}) {{
id
}}
}}
"""
update_payload = payload.replace("\\\\\"", "\"")
print(update_payload)
data = {'query' : update_payload}
r_boards = requests.post(url=apiUrl, headers=headers, data=json.dumps(data)) # make request
return r_boards
# Iterate through each group
for idx, (g_name, g_id) in enumerate(gid_dict.items()):
if g_name == "Group Title":
continue
df_filter_project = df.loc[df['Project'] == g_name]
item_name_list = df_filter_project['Task'].tolist()
item_vals_df = df_filter_project.drop(['Project', 'Task'], axis=1)
# Iterate through each
# Iterate through each row and construct the string
formatted_strings = []
for _, row in item_vals_df.iterrows():
data_dict = row.to_dict()
# Not ideal to have nested for loops (shouldn't be too slow since it's looking at columns and not records)
replaced_dict = {}
for key1 in data_dict.keys():
for key2 in cid_dict.keys():
if key1 == key2:
replaced_dict[cid_dict[key2]] = data_dict[key1]
# Construct the string for each row
formatted_string = "{"
for col, value in replaced_dict.items():
formatted_string += "\\\"{}\\\":{{\\\"text\\\":\\\"{}\\\"}},".format(col, value)
formatted_string = formatted_string[:-2] # Remove the trailing comma and space
formatted_string += "}"
print(create_item(created_board_id, g_id, item_name, formatted_string).json())
sys.exit()
Print Outputs
Those “field#” are the ids of the related columns
mutation {
create_item (board_id: xxx group_id: "xxx" item_name: "xxx" column_values: "{\"field1\":{\"text\":\"xxx\"},\"field2\":{\"text\":\"xxx\"},\"field3\":{\"text\":\"xxx\"},\"field4\":{\"text\":\"xxx\"},\"field5\":{\"text\":\"xxx\"},\"field6\":{\"text\":\"xxx\"},\"field7\":{\"text\":\"xxx\"},\"field8\":{\"text\":\"xxx\"},\"field9\":{\"text\":\"xxx\"},\"field10\":{\"text\":\"xxx\"},\"field11\":{\"text\":\"xxx\"},\"field12\":{\"text\":\"xxx\"},\"field13\":{\"text\":\"xxx\"},\"field14\":{\"text\":\"xxx\"},\"field15\":{\"text\":\"xxx\"},\"fiel16\":{\"text\":\"xxx\"},\"fiel17\":{\"text\":\"xxx\"},\"field18\":{\"text\":\"xxx\"}") {
id
}
}
{'error_message': '809: unexpected token at \'{"field1":{"text":"xxx"},"field2":{"text":"English, John"},"field3":{"text":"xxx"},"field4":{"text":"xxx"},"field5":{"text":"xxx"},"field6":{"text":"xxx"},"field7":{"text":"xxx"},"field8":{"text":"xxx"},"field9":{"text":"xxx"},"field10":{"text":"xxx"},"field11":{"text":"xxx"},"field12":{"text":"xxx"},"field13":{"text":"xxx"},"field14":{"text":"xxx"},"field15":{"text":"xxx"},"field16":{"text":"xxx"},"field17":{"text":"xxx"},"field18":{"text":"xxx"}\'', 'status_code': 500}