Home > Software engineering >  How to deserialize a Json Response which has dynamic key value pairs
How to deserialize a Json Response which has dynamic key value pairs

Time:10-22

I have a response currently like below, and I want it to make it in one line.

Current Reponse:

[
   {
      "user_id":30826889,
      "hr":{
         "1664325660":65,
         "1664325720":65,
         "1664325780":70
      },
      "rr":{
         "1664325660":18,
         "1664325720":17,
         "1664325780":15
      },
      "snoring":{
         "1664325660":0,
         "1664325720":0,
         "1664325780":0
      }
   },
   {
      "user_id":30826889,
      "hr":{
         "1664340780":72,
         "1664340840":70,
         "1664340900":71,
         "1664340960":70,
         "1664341020":67,
         "1664341080":71,
         "1664341140":69,
         "1664341200":68,
         "1664341260":66,
         "1664341320":68
      },
      "rr":{
         "1664340780":20,
         "1664340840":20,
         "1664340900":19,
         "1664340960":20,
         "1664341020":19,
         "1664341080":19,
         "1664341140":19,
         "1664341200":21,
         "1664341260":22,
         "1664341320":22
      },
      "snoring":{
         "1664340780":0,
         "1664340840":0,
         "1664340900":0,
         "1664340960":0,
         "1664341020":0,
         "1664341080":0,
         "1664341140":0,
         "1664341200":0,
         "1664341260":0,
         "1664341320":0
      }
   }
]

and so on....

I want it like below to make it key value-pairs. Like this;

{"user_id": 30826889, "timestamp": "166432xxxx","hr":65, "rr":45, "snoring":1 }
{"user_id": 30826889, "timestamp": "166432yyyy","hr":67, "rr":23, "snoring":2 }

and So on.... for every response..

I tried many things but couldn't succeeded. Please guide, how can i achieve above.. ............................................

CodePudding user response:

Your question is not clear. Tell me whether this output is what you want.

https://github.com/octomix/josson

Josson josson = Josson.fromJsonString(
    "{"  
    "    \"user_id\": 30826889,"  
    "    \"hr\": {"  
    "        \"1664325660\": 65,"  
    "        \"1664325720\": 65,"  
    "        \"1664325780\": 70"  
    "    },"  
    "    \"rr\": {"  
    "        \"1664325660\": 18,"  
    "        \"1664325720\": 17,"  
    "        \"1664325780\": 15"  
    "    },"  
    "    \"snoring\": {"  
    "        \"1664325660\": 0,"  
    "        \"1664325720\": 1,"  
    "        \"1664325780\": 2"  
    "    }"  
    "}");
JsonNode node = josson.getNode(
    "map(user_id,"  
    "    items: collect(hr.entries().map(key, hr:value),"  
    "                   rr.entries().map(key, rr:value),"  
    "                   snoring.entries().map(key, snoring:value))"  
    "           .flatten()"  
    "           .group(key, field(key:))"  
    "           .map(timestamp:key, elements.hr[0], elements.rr[0], elements.snoring[0])"  
    ")"  
    ".unwind(items)");
System.out.println(node.toPrettyString());

System.out.println("\nLoop through each array element");
for (JsonNode elem : node) {
    System.out.println("-----");
    System.out.println(elem.toPrettyString());
}

Output

[ {
  "user_id" : 30826889,
  "timestamp" : "1664325660",
  "hr" : 65,
  "rr" : 18,
  "snoring" : 0
}, {
  "user_id" : 30826889,
  "timestamp" : "1664325720",
  "hr" : 65,
  "rr" : 17,
  "snoring" : 1
}, {
  "user_id" : 30826889,
  "timestamp" : "1664325780",
  "hr" : 70,
  "rr" : 15,
  "snoring" : 2
} ]

Loop through each array element
-----
{
  "user_id" : 30826889,
  "timestamp" : "1664325660",
  "hr" : 65,
  "rr" : 18,
  "snoring" : 0
}
-----
{
  "user_id" : 30826889,
  "timestamp" : "1664325720",
  "hr" : 65,
  "rr" : 17,
  "snoring" : 1
}
-----
{
  "user_id" : 30826889,
  "timestamp" : "1664325780",
  "hr" : 70,
  "rr" : 15,
  "snoring" : 2
}

CodePudding user response:

public static void main(String... args) throws IOException {
    File src = new File("e:/in.json");
    ObjectMapper mapper = new ObjectMapper();
    List<Map<String, Object>> items = mapper.readValue(src, new TypeReference<>() {
    });

    for (Map<String, Object> map : convert(items))
        System.out.println(mapper.writeValueAsString(map));
}

public static List<Map<String, Object>> convert(List<Map<String, Object>> items) {
    return items.stream()
                .flatMap(item -> convert(item).stream())
                .collect(Collectors.toList());
}

private static List<Map<String, Object>> convert(Map<String, Object> item) {
    return getUniqueTimes(item.values()).stream()
                                        .map(time -> createTimeItem(item, time))
                                        .collect(Collectors.toList());
}

private static Set<String> getUniqueTimes(Collection<Object> values) {
    return values.stream()
                 .filter(value -> value instanceof Map)
                 .flatMap(value -> ((Map<String, Integer>)value).keySet().stream())
                 .collect(Collectors.toSet());
}

private static Map<String, Object> createTimeItem(Map<String, Object> item, String timestamp) {
    Map<String, Object> map = new LinkedHashMap<>();
    map.put("user_id", item.get("user_id"));
    map.put("timestamp", timestamp);

    item.entrySet().stream()
        .filter(entry -> entry.getValue() instanceof Map)
        .forEach(entry -> map.put(entry.getKey(), ((Map<String, Integer>)entry.getValue()).get(timestamp)));

    return map;
}

Demo

{"user_id":30826889,"timestamp":"1664325720","hr":65,"rr":17,"snoring":0}
{"user_id":30826889,"timestamp":"1664325780","hr":70,"rr":15,"snoring":0}
{"user_id":30826889,"timestamp":"1664325660","hr":65,"rr":18,"snoring":0}
{"user_id":30826889,"timestamp":"1664340900","hr":71,"rr":19,"snoring":0}
{"user_id":30826889,"timestamp":"1664341260","hr":66,"rr":22,"snoring":0}
{"user_id":30826889,"timestamp":"1664340840","hr":70,"rr":20,"snoring":0}
{"user_id":30826889,"timestamp":"1664341080","hr":71,"rr":19,"snoring":0}
{"user_id":30826889,"timestamp":"1664341320","hr":68,"rr":22,"snoring":0}
{"user_id":30826889,"timestamp":"1664341200","hr":68,"rr":21,"snoring":0}
{"user_id":30826889,"timestamp":"1664340960","hr":70,"rr":20,"snoring":0}
{"user_id":30826889,"timestamp":"1664341140","hr":69,"rr":19,"snoring":0}
{"user_id":30826889,"timestamp":"1664341020","hr":67,"rr":19,"snoring":0}
{"user_id":30826889,"timestamp":"1664340780","hr":72,"rr":20,"snoring":0}
  • Related