Converting DynamoDB JSON to Standard JSON with Java

后端 未结 5 929
说谎
说谎 2020-12-16 06:36

I need to convert a AWS DYNAMODB JSON to a standard JSON object. so I can remove the data type from the DynamoDB JSON Something more like:

in DYNAMODB JSON:

5条回答
  •  无人及你
    2020-12-16 07:26

    Amazon has an internal utility that can do the core of the job in 1 line.

    Map jsonMapWithId = InternalUtils.toSimpleMapValue(attributeValueMap);
    

    A long version which contains some configurations:

    import com.amazonaws.services.dynamodbv2.document.internal.InternalUtils;
    import com.amazonaws.services.dynamodbv2.model.AttributeValue;
    import java.lang.Map;
    import java.lang.HashMap;
    
    public class JsonConvert {
        public static void main(String[] args) {
            Map jsonMap = new HashMap();
            jsonMap.put("string", "foo");
            jsonMap.put("number", 123);
            Map innerMap = new HashMap();
            innerMap.put("key", "value");
            jsonMap.put("map", innerMap);
            AttributeValue attributeValue = InternalUtils.toAttributeValue(jsonMap);
            Map attributeValueMap = new HashMap();
            attributeValueMap.put("id", attributeValue);
    
            Map jsonMapWithId = InternalUtils.toSimpleMapValue(attributeValueMap);
            Map jsonMap = jsonMapWithId.get("id"); // This is a regular map, values are not Amazon AttributeValue
    
            Gson gson = new Gson(); 
            String json = gson.toJson(jsonMap); 
    
            System.out.println(json);
        }
    }
    

提交回复
热议问题