REST API with python to update multiple columns

Hi All,

I am struggling getting multiple column updates using a rest API in python

I am using a mutation query as follows:

query = f'''
       mutation ($column_values: JSON!) {{
       change_multiple_column_values(board_id: {board_id}, item_id: {item_id}, column_values: $column_values) {{
                    id
                  }}
                }}

I am using a dictionary of column names and values
column_values = {“col1”: “value1”, “col2”: "value2 …more}

I place it in a json string as follows

column_values_json = json.dumps(column_values)

I then add that to my variables field for the python request.

variables = {'column_values': column_values_json}

I then use this helper function to create my request.

def api_request(query, variables=None, headers=None):
    """Helper function to make monday.com API requests."""

    if variables is None:
        variables = {}
    data = {"query": query, "variables": variables}
    try:
        response = requests.post(url=API_URL, json=data, headers=headers)
        response.raise_for_status() # Raise an exception for bad status codes
        return response.json()
    except requests.exceptions.RequestException as e:
        print(query)
        print(variables)
        print(f"API request failed: {e}")
        return None

I don’t think it likes the string created by json.dumps() in the as the value of column_values.

this variable string works in the playground

{"column_values": "{\"project_owner\": \"johndoe@someemail.com\"}

However I cannot seem to get my python string formatted correctly for the REST API queries. Has any had success with this using python?

Hello @mseaborn The issue is double-encoding.

monday expects column_values as a JSON string, but if you json.dumps() it and then pass it again using json=... in requests.post, it gets escaped.

Do this:

Build your dict
column_values = {"col1": "value1", "col2": "value2"}

Stringify once
variables = {"column_values": json.dumps(column_values)}

In your mutation, use JSON type
$column_values: JSON!

Example mutation shape:
change_multiple_column_values(board_id: $board_id, item_id: $item_id, column_values: $column_values)

Dr. Tanvi Sachar
Monday Certified Partner, Monday Wizard

1 Like