Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python group JSON Response by ID

I call a restAPI via Python and get a JSON Object in Response which looks like this:

[
   {
      "userid":1,
      "check_in":"2022-01-10T07:00:00.000Z",
      "calculated":false
   },
   {
      "userid":1,
      "check_in":"2022-01-10T11:00:00.000Z",
      "calculated":false
   },
   {
      "userid":1,
      "check_in":"2022-01-10T12:00:00.000Z",
      "calculated":false
   },
   {
      "userid":1,
      "check_in":"2022-01-10T16:00:00.000Z",
      "calculated":false
   },
   {
      "userid":2,
      "check_in":"2022-01-10T07:00:00.000Z",
      "calculated":false
   },
   {
      "userid":2,
      "check_in":"2022-01-10T11:15:00.000Z",
      "calculated":false
   },
   {
      "userid":2,
      "check_in":"2022-01-10T12:00:00.000Z",
      "calculated":false
   },
   {
      "userid":2,
      "check_in":"2022-01-10T16:30:00.000Z",
      "calculated":false
   }
]

I am struggling with the problem to group those Values means I want to achieve something like this:

[
   {
      "userid":1,
      "check_in":[
         "2022-01-10T07:00:00.000Z",
         "2022-01-10T11:00:00.000Z",
         "2022-01-10T12:00:00.000Z",
         "2022-01-10T16:00:00.000Z"
      ],
      "calculated":false
   },
   {
      "userid":2,
      "check_in":[
         "2022-01-10T11:00:00.000Z",
         "...",
         "...."
      ],
      "calculated":false
   }
]

Would be great if someone could point me out how to achieve this in python since I usually don´t develop python.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks in advance!

>Solution :

I used groupby from itertools module to solve this. Groupby the (userid, calculated) pair and the extract the data from the groupings.

import json
from itertools import groupby
data = [
   {
      "userid":1,
      "check_in":"2022-01-10T07:00:00.000Z",
      "calculated":False
   },
   {
      "userid":1,
      "check_in":"2022-01-10T11:00:00.000Z",
      "calculated":False
   },
   {
      "userid":1,
      "check_in":"2022-01-10T12:00:00.000Z",
      "calculated":False
   },
   {
      "userid":1,
      "check_in":"2022-01-10T16:00:00.000Z",
      "calculated":False
   },
   {
      "userid":2,
      "check_in":"2022-01-10T07:00:00.000Z",
      "calculated":False
   },
   {
      "userid":2,
      "check_in":"2022-01-10T11:15:00.000Z",
      "calculated":False
   },
   {
      "userid":2,
      "check_in":"2022-01-10T12:00:00.000Z",
      "calculated":False
   },
   {
      "userid":2,
      "check_in":"2022-01-10T16:30:00.000Z",
      "calculated":False
   }
]

result = []
for group, checkings in groupby(data, lambda x: (x["userid"], x["calculated"])):
        user = {
                "userid": group[0],
                "calculated": group[1],
                "check_in": []
        }
        for check_in in checkings:
                user["check_in"].append(check_in["check_in"])
        result.append(user)

print(json.dumps(result, indent=2))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading