将DynamoDB JSON文档转换为Java中的JSON对象 [英] Converting DynamoDB JSON document to JSON object in Java

查看:67
本文介绍了将DynamoDB JSON文档转换为Java中的JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读取备份DynamoDB S3存储桶,该存储桶具有DynamoDB Json的格式.

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

我正在尝试将其转换为不带AttributeValue的普通Json.

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

原始字符串

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

尝试转换为

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

我提到了很多答案,但它并没有转换成普通的Json,它给了我同样的Json.

There are many answers which I referred to but it doesnt convert into normal Json, it gives me back the same Json.

这是我尝试过的:

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()));

此外,在调试时,我无法正确创建Map.映射将包含键作为"id",值包含AttributeValue.但是 AttributeValue 会将字符串包含在其自己的 Map< String,AttributeValue> 内部,而不是在 String s

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?

有关Stackoverflow的参考:链接1 链接2

References on Stackoverflow: Link 1 Link 2

谢谢

周杰伦

推荐答案

您所描述的结果听起来是正确的.如果您查看原始的JSON字符串,则会有一个key:value对列表,其中每个值都是key:value对的另一个列表(长度为1,但仍然是一个列表).

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).

要将其转换为key:value对的单个列表,您需要编写map-reduce类型的循环.基本上迭代第一个映射,获取键,然后从第二个映射(即您刚刚记录的键的值)中,获取第一个条目的值(AttributeValue),然后将key:value对添加到新映射中( Map< String,AttributeValue> ,您在循环之前定义了该代码).然后,您可以将该地图转换为JSON,它将生成您期望的JSON字符串.

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.

希望有帮助!

这篇关于将DynamoDB JSON文档转换为Java中的JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆