3

I have a table that has an attribute which is actually a JSON list.

var myItem = {
    name: 'myname',
    tasks: [
       { key1: 'value1', key2: 'value2' }
    ]
}

I put the myItem into the table like this, and this seems to work fine.

var params = {
    TableName: 'runs',
    Item: myItem
};
dynamodbDoc.put( params, ...

I then want to update the record to change the "tasks".

var newTasks = [
   { key1: 'newvalue1', key2: 'newvalue2' }
]

So I setup for an updateItem like this:

var params = {
    TableName: 'runs',
    Key: {
        name: { S: 'myname' }
    },
    UpdateExpression: "SET #tasks = :tasks",
    ExpressionAttributeNames: {
        "#tasks": "tasks"
    },
    ExpressionAttributeValues: {
        ":tasks": newTasks
    },
    ReturnValues: 'ALL_NEW'
}

dynamodb.updateItem( params, ...

This fails by giving me UnexpectedParameter: Unexpected key '0' found in params.ExpressionAttributeValues[':tasks'].

I've also tried

ExpressionAttributeValues: {
    ":tasks": { M: tasks }
},

And also...

ExpressionAttributeValues: {
    ":tasks": { L: tasks }
},

These don't work either. Obviously I'm missing something here.

zsimpson
  • 765
  • 2
  • 9
  • 13

1 Answers1

2

If you're using the low level api calls then you need to fully serialize your data to the proper dynamodb attribute values, including the values nested in the list.

ExpressionAttributeValues: {
  ":tasks": {L:[ 
                {M : {key1 : {S : "newvalue1"}, 
                      key2 : {S : "newvalue2"} 
                }
            ]}
}

As you can imagine serializing out this data by hand can get tiring really fast, I would suggest looking into using a high level wrapper which will automatically perform the serialization for you.

Ryan Fitzgerald
  • 819
  • 5
  • 6
  • Thanks so much for the clue! Problem was I was getting the different interfaces mixed up. When I switch to use the high-level DocumentClient API update() function instead of the low-level dynamoDB updateItem() then all started working as expected. – zsimpson Dec 02 '15 at 16:47
  • @ryan check [this](http://stackoverflow.com/questions/40317443/updating-a-json-array-in-aws-dynamodb). I need to update a specific JSON in an array. Something quite like yours. I could not find a proper resource to do that. – Lakshman Diwaakar Nov 05 '16 at 02:58