2

I have a JSONObject store on dynamodb as following,

"info" :  {
        "nn": {
            "nnn": [{
                "nnnn": "bb"
            }]
        },
        "ln": "zheng1",
        "fn": "franky",
        "sn": [{
            "aa": "zheng2"
        }]
    }

I would like to update info.sn by UpdateItemSpec

    List<JSONObject> li= new ArrayList<JSONObject>();
    JSONObject j = new JSONObject();
    j.append("kk","kkkk");
    li.add(j);

UpdateItemSpec updateItemSpec = new UpdateItemSpec()
        .withPrimaryKey("Age", age, "Name", name)
        .withExpressionSpec(new ExpressionSpecBuilder()
                    .addUpdate(ExpressionSpecBuilder.S("phone").set("0900000222"))
                    .addUpdate(ExpressionSpecBuilder.S("info.ln").set("zheng32"))
                    .addUpdate(ExpressionSpecBuilder.list_append("info.sn", li))  // ERRO
                    .withCondition(ExpressionSpecBuilder.N("weight").eq(82))
                    .buildForUpdate())
        .withReturnValues(ReturnValue.UPDATED_NEW);

error will be list_append, I cannot find out the way to update JSONArray, someone can help?

2 Answers2

0

This answer should be helpful. So your update item spec could look like following

UpdateItemSpec updateItemSpec = new UpdateItemSpec()
            .withPrimaryKey("Age", age, "Name", name)
            .withNameMap(new NameMap().with("#P", "info.sn"))
            .withValueMap(new ValueMap()
                    .withList(":val", li) //list of map
                    .withList(":empty_list", new ArrayList<>()))
            .withUpdateExpression("SET #P = list_append(if_not_exists(#P, :empty_list), :val)");

I tried using ExpressionSpecBuilder at first but I failed too

Community
  • 1
  • 1
jasiustasiu
  • 1,468
  • 2
  • 20
  • 28
0

Honestly the Java client is a monument to Java anti-patterns.

Here is an example of list_append using the ExpressionSpecBuilder style.

final UpdateItemExpressionSpec xspec = new ExpressionSpecBuilder()
        .addUpdate(
                ExpressionSpecBuilder.L("log")
                .set(ExpressionSpecBuilder.L("log") // yes you have to repeat the column name
                .listAppend(value)))
        .buildForUpdate();

final UpdateItemOutcome updateItemOutcome = table.updateItem("Id", id, xspec);
foo
  • 701
  • 4
  • 8