0

I have the follow attribute in a DynamoDB table (trust me, I know it's a mess):

"list1": [
  {
    "mapName": "map1",
    "list2": [
      {
        "mapName": "map2",
        "list3": [ #append map here
        ]
      }
    ]
  }
]

I need to append a map to list3, and I am struggling with the syntax of how to make update_item do this. The position in list1 and list2 are stored in variables i and j respectively. I am trying something like this:

table.update_item(
    Key={
        'primary_key': item_name
    },
    UpdateExpression="SET list1[:i].list2[:j].list3 = list_append(list1[:i].list2[:j].list3, :x)",
    ExpressionAttributeValues={
        ':x': map,
        ':i': i,
        ':j': j
    }
)

The error message I am getting with the code is at the update_item line:

"errorMessage": "An error occurred (ValidationException) when calling the UpdateItem operation: Invalid UpdateExpression: Syntax error; token: \":i\", near: \"[:i]\"",

I'm guessing I will need some combination of ExpressionAttributeNames and ExpressionAttributeValues, but I am not very familiar with boto3 syntax. Any help on how to accomplish this would be greatly appreciated.

Xela
  • 71
  • 8
  • 1
    What is the result of the code you've shown? Also, see https://stackoverflow.com/questions/51911927/update-nested-map-dynamodb – jarmod Jun 25 '21 at 15:34
  • Thanks for the response! The error message I am getting with the code is at the update_item line: "errorMessage": "Unsupported type \"\" for value \"\"", "errorType": "TypeError", And thanks for that link, I will see if it sheds any light. – Xela Jun 25 '21 at 15:37
  • Apologies, listed the wrong error, I have updated it again accordingly. – Xela Jun 25 '21 at 15:54

1 Answers1

0

So I managed to do what I wanted as follows:

string = f"SET list1[{i}].list2[{j}].list3 = list_append(list1[{i}].list2[{j}].list3, :x)"
            
table.update_item(
    Key={
        'primary_key': item_name
    },
    UpdateExpression=string,
    ExpressionAttributeValues={
        ':x': [map]
    }
)

Not the most elegant solution, if anyone can show how to do this without the fstring, please post an additional answer.

Xela
  • 71
  • 8
  • There are 2 changes here: 1) using the f-string and 2) changing `':x': map` to `':x': [map]`). Were both required, or just the latter? – jarmod Jun 28 '21 at 14:57
  • Hey jarmod, yes, both changes were required. The f-string got around the syntax issues with the UpdateExpression, and the [map] got around another issue with list_append; as I understand it, list_append takes two lists as arguments so :x does need to be a list object. Please let me know if I can expand any more. – Xela Jun 28 '21 at 15:32