Search code examples
javajsonjacksongsonjson-deserialization

How to deserialize a Json Response which has dynamic key value pairs


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


Solution

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

    https://github.com/octomix/josson

    Deserialization

    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\":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" +
        "      }" +
        "   }" +
        "]");
    

    Transformation

    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)");
    
    for (JsonNode elem : node) {
        System.out.println(elem.toString());
    }
    

    Output

    {"user_id":30826889,"timestamp":"1664325660","hr":65,"rr":18,"snoring":0}
    {"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":"1664340780","hr":72,"rr":20,"snoring":0}
    {"user_id":30826889,"timestamp":"1664340840","hr":70,"rr":20,"snoring":0}
    {"user_id":30826889,"timestamp":"1664340900","hr":71,"rr":19,"snoring":0}
    {"user_id":30826889,"timestamp":"1664340960","hr":70,"rr":20,"snoring":0}
    {"user_id":30826889,"timestamp":"1664341020","hr":67,"rr":19,"snoring":0}
    {"user_id":30826889,"timestamp":"1664341080","hr":71,"rr":19,"snoring":0}
    {"user_id":30826889,"timestamp":"1664341140","hr":69,"rr":19,"snoring":0}
    {"user_id":30826889,"timestamp":"1664341200","hr":68,"rr":21,"snoring":0}
    {"user_id":30826889,"timestamp":"1664341260","hr":66,"rr":22,"snoring":0}
    {"user_id":30826889,"timestamp":"1664341320","hr":68,"rr":22,"snoring":0}