7

Github GitHub - bijukunjummen/aws-sdk2-dynamo-json-helper: Java Library to conve...

 3 years ago
source link: https://github.com/bijukunjummen/aws-sdk2-dynamo-json-helper
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Jackson JsonNode to AWS SDK 2 AttributeValue

This is a small java library that provides a utility method to convert a Jackson JsonNode to an AWS SDK 2.x AttributeValue and back.

Using the library:

Add a dependency, for eg. with gradle:

implementation("com.github.bijukunjummen:aws-sdk2-dyanamo-json-helper:$VERSION")

Now, in the code, given a json which looks like this:

{
  "key1": "value1",
  "key2": [
    "arr1",
    "arr2"
  ],
  "level1": {
    "level2": {
      "level3": {
        "l3Key": "l3Value"
      }
    }
  }
}

The Json can be converted to an AttributeValue the following way:

JsonNode jsonNode = objectMapper.readTree(json);
AttributeValue dataAttributeValue = JsonAttributeValueUtil.toAttributeValue(jsonNode);

The utility recurses the json structure and creates the nested AttributeValue structure.

Once it is saved to dynamoDB, after retrieving the content, it can be converted back to a json the following way:

AttributeValue attributeValue = response.item().get("data");
JsonNode dataJsonNode = JsonAttributeValueUtil.fromAttributeValue(attributeValue);

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK