9

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.

Matthew Pope
  • 7,212
  • 1
  • 28
  • 49

3 Answers3

-1

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.

Nazeel
  • 326
  • 3
  • 10
-1

The InternalUtils is deprecated but the code below works fine and also shows, how you can convert that to PutItemResult:

HashMap responseMap = objectMapper.readValue(responseBody, HashMap.class);
            Map<String, AttributeValue> attributes = InternalUtils.fromSimpleMap(responseMap);

            PutItemResult result = new PutItemResult();
            result.setAttributes(attributes);
Arefe
  • 11,321
  • 18
  • 114
  • 168
-1

Here are a couple ways to do this (in Kotlin) depending on whether you have simple JSON or the DynamoDB json:

import com.amazonaws.services.dynamodbv2.document.ItemUtils
import com.amazonaws.services.dynamodbv2.model.AttributeValue
import com.fasterxml.jackson.core.type.TypeReference

...
val objectMapper = ObjectMapper().apply {
    configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true)
}

...

// Method 1 - simple JSON
val obj1: MutableMap<String, AttributeValue> = ItemUtils.fromSimpleMap(
    objectMapper.readValue(
        """
                {
                 "fruits": ["apple", "pear"],
                 "user": { "name": "Bob" }
                }
            """,
        object : TypeReference<Map<String, Any>>() {})
)

// Method 2 - DynamoDB JSON
val obj2: Map<String, AttributeValue> =
    objectMapper.readValue("""
        {
         "fruits": {
          "L": [
             { 
              "S": "apple"
             },
             { 
              "S": "pear"
             }
          ]
         },
         "user": {
            "M": {
             "name": {
              "S": "Bob"
             }
            }
           }
        }
    """.trimIndent(),
    object : TypeReference<Map<String, AttributeValue>>() {}
)
mowwwalker
  • 16,634
  • 25
  • 104
  • 157