Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert between Json and Map<String, AttributeValue> in

I want to save an object that is encoded as a Json string to DynamoDB using the AWS Java SDK 2.0.

In the AWS Java SDK 1.n, it is possible to convert standard Json strings to DynamoDB AttributeValues using Item.fromJSON(myJsonString).toAttributeValues().

Though it is possible to use both SDKs at once, the AttributeValue defined by the two SDK versions (1.11, 2.0) are not the same and cannot be used interchangeably.

Is there any AWS-provided or community-standard way to go from a json string/blob to a Map<String, AttributeValue> for the AWS Java SDK 2.0?


Please note—this question is asking about how to solve the problem for AWS Java SDK 2.0, not the dynamodbv2 model of AWS Java SDK 1.n. If you think this question is a duplicate, please double check the SDK version of the question/answer that it duplicates.

like image 768
Matthew Pope Avatar asked Nov 06 '22 09:11

Matthew Pope


1 Answers

An example for converting JSON to AttributeValue of DynamoDB

    String productString = readFileFromResources("data/categoryFix.json");
    HashMap<String,Object> result = new ObjectMapper().readValue(productString, HashMap.class);
    Map<String, AttributeValue> attributes = InternalUtils.fromSimpleMap(result);

Above code worked well, even though InternalUtils is deprecated.

like image 72
Nazeel Avatar answered Nov 14 '22 06:11

Nazeel