1

I am reading the backup DynamoDB S3 bucket which has format for DynamoDB JSON.

I am trying to convert it into a normal JSON without the AttributeValue.

Original String

{
  "id": {
    "s": "someString"
  },
  "name": {
    "b": "someByteBuffer"
  },
  "anotherId": {
    "s": "someAnotherString"
  },
  "version": {
    "n": "1"
  }
}

Trying to convert to

{
  "id": "someString",
  "name": "someByteBuffer",
  "anotherId": "someAnotherString",
  "version": "1"
}

There are many answers which I referred to, but it doesn't convert into normal JSON, it gives me back the same JSON.

Here is what I tried:

ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(inputJsonString);
final JacksonConverter converter = new JacksonConverterImpl();
Map<String, AttributeValue> map = converter.jsonObjectToMap(jsonNode);        

Item item = ItemUtils.toItem(map);

Gson gson = new Gson();
System.out.println(gson.toJson(item.asMap()));

Also, when I was debugging, I wasn't able to create the Map properly. Map would contain key as "id", value as AttributeValue, but the AttributeValue would contain the string inside its own Map<String, AttributeValue> instead of inside String s

I feel I am missing something while creating the Map. Any pointers?

References on Stackoverflow: Link 1 Link 2

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Jay Patel
  • 1,266
  • 6
  • 20
  • 38
  • Does this answer your question? [Converting DynamoDB JSON to Standard JSON with Java](https://stackoverflow.com/questions/43812278/converting-dynamodb-json-to-standard-json-with-java) – Alex R Feb 25 '22 at 07:06

1 Answers1

0

What you're describing as the result sounds correct. If you look at the original JSON string, you have a list of key:value pairs where each value is another list of key:value pairs (length 1, but still a list).

To convert this to a single list of key:value pairs you need to write a map-reduce type of loop. Basically iterate the first map, take the key, then, from the second map (which is the value for the key you just recorded), just take the first entry's value (AttributeValue) and add that key:value pair into a new map (Map<String, AttributeValue>, which you define before the loop). That map you can then convert to JSON and it will result in the JSON string that you were expecting.

Hope that helps!

PVDM
  • 98
  • 7
  • From reading the docs, https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodbv2/model/AttributeValue.html, it seems you may want to take the AttributeValue's value, convert it to a String and then add it to a `Map` instead of a `Map`. Try both and see what your result is. – PVDM Jul 14 '18 at 00:48