Venedictos
Venedictos

Reputation: 17

Accessing different JSON key/value pairs in a list comprehension

Not sure if i am trying to achieve the impossible? I have this JSON string:

dot= [{"type": 1, "date": "2018-12-02T00:40:03.2186792+00:00", "device": 
[{"id": "20165cf4e596", "deviceName": "17", "records": [{"timestamp": "2018- 
12-02T00:40:00.499+00:00", "grp": "undefined", "val": 887}]}, {"id": 
"5f401a6a6f66", "deviceName": "18", "records": [{"timestamp": "2018-12- 
02T00:42:00.499+00:00", "grp": "undefined", "val": 1063}, {"timestamp": 
"2018-12-02T00:41:00.498+00:00", "grp": "undefined", "val": 907}]}, {"id": 
"569bb0147a72", "deviceName": "19", "records": [{"timestamp": "2018-12- 
02T00:44:00.499+00:00", "grp": "undefined", "val": 817}, {"timestamp": 
"2018-12-02T00:43:00.498+00:00", "grp": "undefined", "val": 1383}]}, {"id": 
"ef829aa3", "deviceName": "2", "records": [{"timestamp": "2018-12- 
02T00:46:00.499+00:00", "grp": "undefined", "val": 1173}]}, {"id": 
"388ae8f2fa64", "deviceName": "17", "records": [{"timestamp": "2018-12- 
02T00:41:00.499+00:00", "grp": "undefined", "val": 866}, {"timestamp": 
"2018-12-02T00:32:00.492+00:00", "grp": "undefined", "val": 1080}]}, {"id": 
"01f874b30b55", "deviceName": "19", "records": [{"timestamp": "2018-12- 
02T00:43:00.499+00:00", "grp": "undefined", "val": 1050}, {"timestamp": 
"2018-12-02T00:42:00.498+00:00", "grp": "undefined", "val": 1084}]}]}]

And i want to achieve the following:

[{'id': '20165cf4e596','deviceName': '17','timestamp': '2018-12- 
02T00:40:00.499+00:00','grp': 'undefined','val': 887},
{'id': '5f401a6a6f66','deviceName': '18','timestamp': '2018-12- 
02T00:42:00.499+00:00','grp': 'undefined','val': 1063},
{'id': '5f401a6a6f66','deviceName': '18','timestamp': '2018-12- 
02T00:41:00.498+00:00','grp': 'undefined','val': 907},...]

I used the following code:

for i in dot:
    for k in i['device']:
        d2= [[{l:m},{'value':v}] for l,m in k.items() for p in m if 
        isinstance(p,list) for v in p]
        print(d2)

And got empty lists:

[]
[]
[]
[]

Thanks in advance

Upvotes: 0

Views: 135

Answers (2)

alex
alex

Reputation: 7433

Barmar's answer may be correct - but I believe you will have a far easier time if you deobfuscate your code by creating a hierarchy of objects which represent items in your data structure:

class SomeObject:
    def __init__(self, some_object_type, date, devices):
        self.type = some_object_type
        self.date = date
        self.devices = []

        for d in devices:
            some_device = SomeDevice(d["id"], d["deviceName"], d["records"])
            self.devices.append(some_device)

class SomeDevice:
    def __init__(self, some_device_id, name, records):
        self.id = some_device_id
        self.name = name
        self.records = []

        for r in records:
            some_record = SomeDeviceRecord(r["timestamp"], r["grp"], r["val"])
            self.records.append(some_record)

class SomeDeviceRecord:
    def __init__(self, timestamp, group, value):
        self.timestamp = timestamp
        self.group = group
        self.value = value

If you go this route, you can easily parse the entire JSON into strongly-typed objects:

some_object = SomeObject(dot[0]["type"], dot[0]["date"], dot[0]["device"])

And then it should be fairly easy/straightforward to report on specific portions of the data structure:

for device in some_object.devices:
    print(device.id, device.name, device.records[0].timestamp, device.records[0].group, device.records[0].value)

Upvotes: 0

Barmar
Barmar

Reputation: 780688

You need to loop over the records element of each device. Then combine the fields from the device with each record.

result = []
for i in dot:
    for k in i['device']:
        for r in k['records']:
            result.append({"id": k["id"], "deviceName": k["deviceName"], "timestamp": r["timestamp"], "grp": r["grp"], "val": r["val"]})
print(result)

Upvotes: 2

Related Questions