How to uniformize dictionary keys casing in Python

61 Views Asked by At

I have a consumer lambda, that consumes data from two different services, that provide the "same-ish" reposes, that I'll need to reconcile, for instance:

Response from service A: (There will be N items in each array, showing one as example).

[{
    "property_type": "something",
    "property": "something_nah",
    "property_context": {
        "some_thing": "nasty"
    },
    "value": null,
    "priority": 42
}]

Response from service B:

[{
    "propertyType": "something",
    "propertyId": "something_different",
    "propertyContext": {
        "someThing": "even more nasty",
        "anotherThing": "Unlimited power ... nested items"
    },,
    "value": null,
    "priority": 12
}]

Now, I need to create a single list with those responses, order and filter based on their property_type/propertyType, context and Id. Basically, I need to sanitise/uniformize the casing on the property names.

Being a Typescript/C# developer I can imagine some ways of doing this but all of them incur into iterating over each key recursively to fix the casing but I heard there's something called "Pythonic" way of doing stuff that could help me here. So here's the question:

How can I uniformize the property name casing on a requests (lib that does http calls) response?

For instance, transform the response from service A above into this (matching response from service B):

[{
    "propertyType": "something",
    "propertyId": "something_nah",
    "propertyContext": {
        "someThing": "nasty"
    },
    "value": null,
    "priority": 42
}]

This Seems like a solution but I'm looking for something that does not enumerate all the responses (or, at least, postpone it), as they're usually many and I handle tons of them.

I was thinking about something like a special type casting on the requests side, that I could cast it to a type and, I don't know, when serialising such type it would use a different casing on the serialisation, or a Class that receives the response as dict and has getters that do the casing change.

0

There are 0 best solutions below