0

I have a list nested dictionary that looks in the following way and I have an epoch timestamp stored as a variable.

ts = 1653070640

   [ 
      {
        "HR": {
            "EKG": {
                "HR_EKG": 136.0
            },
            "PPG": {
                "HR_PPG_1": 135.0,
                "HR_PULSED": 135.0
            }
        },
        "NIBP": {
            "DBP": {
                "NIBPD": 25.0
            },
            "MBP": {
                "NIBPM": 53.0
            },
            "SBP": {
                "NIBPS": 73.0
            }
        },
        "SPO2": {
            "PI": {
                "PI_1": 4.25
            },
            "SPO2": {
                "SpO2_1": 86.0
            }
        }
    } 
 ]

What I want to do is to add the ts variable as a new key to the innermost nested dictionary. That nested dictionary length may vary so I guess it should be a recursive approach.

    [ 
      {
        "HR": {
            "EKG": {
                "HR_EKG": 136.0
                "timestamp": 1653070640

            },
            "PPG": {
                "HR_PPG_1": 135.0,
                "HR_PULSED": 135.0,
                "timestamp": 1653070640
            }
        },
        "NIBP": {
            "DBP": {
                "NIBPD": 25.0,
                "timestamp": 1653070640
            },
            "MBP": {
                "NIBPM": 53.0,
                "timestamp": 1653070640
            },
            "SBP": {
                "NIBPS": 73.0,
                "timestamp": 1653070640
            }
        },
        "SPO2": {
            "PI": {
                "PI_1": 4.25,
                "timestamp": 1653070640
            },
            "SPO2": {
                "SpO2_1": 86.0,
                "timestamp": 1653070640
            }
        }
    }  
]

What I've tried is the following function that prints all the keys but not sure how to add a new key to the inner most dict.

def get_all_keys(d):
  for key, value in d.items():
     yield key
     if isinstance(value, dict):
        yield from get_all_keys(value)

1 Answer 1

2

You can do this to add timestamp only at depths where there are no sub-dicts:

def set_ts(d, ts):
    any_nested = False
    for k, v in d.items():
        if isinstance(v, dict):
            set_ts(v, ts)
            any_nested = True
    if not any_nested:
        d['timestamp'] = ts

Usage:

for d in dl:
    set_ts(d, ts)

>>> dl
[{'HR': {'EKG': {'HR_EKG': 136.0, 'timestamp': 1653070640},
   'PPG': {'HR_PPG_1': 135.0, 'HR_PULSED': 135.0, 'timestamp': 1653070640}},
  'NIBP': {'DBP': {'NIBPD': 25.0, 'timestamp': 1653070640},
   'MBP': {'NIBPM': 53.0, 'timestamp': 1653070640},
   'SBP': {'NIBPS': 73.0, 'timestamp': 1653070640}},
  'SPO2': {'PI': {'PI_1': 4.25, 'timestamp': 1653070640},
   'SPO2': {'SpO2_1': 86.0, 'timestamp': 1653070640}}}]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.