1

I have the following variants of input data:

show_version_ios_parsed = {
    "version": {
        "uptime": "7 weeks, 6 days, 3 hours, 37 minutes",
    }
}

and

show_version_nxos_parsed = {
    "platform": {
        "kernel_uptime": {"days": 102, "hours": 1, "minutes": 2, "seconds": 13},
    }
}

I need to extract the uptime (or kernel_uptime) key value as a string so that both results look like this:

{'uptime': '7 weeks, 6 days, 3 hours, 37 minutes'}

and

{'uptime': '102 days, 1 hours, 2 minutes, 13 seconds}

I've come up with the following glom spec:

spec = (
    T.items(),
    Iter(
        {
            "uptime": Coalesce(T[1]["uptime"], T[1]["kernel_uptime"]),
        },
    ),
    Merge(),
)

After applying it to the input data I get this:

>>> glom(show_version_ios_parsed, spec)
{'uptime': '7 weeks, 6 days, 3 hours, 37 minutes'}
>>> glom(show_version_nxos_parsed, spec)
{'uptime': {'days': 102, 'hours': 1, 'minutes': 2, 'seconds': 13}}

As you can see it only extracts the uptime key value as it is.

Is there a way to transform the second uptime value to string with glom?

0 Answers0