0

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.

Eduardo Elias Saléh
  • 806
  • 1
  • 11
  • 23
  • Does this answer your question? [Convert all keys in a nested dictionary from camelCase to snake\_case](https://stackoverflow.com/questions/60148175/convert-all-keys-in-a-nested-dictionary-from-camelcase-to-snake-case) – Gameplay Mar 17 '23 at 11:21
  • @Gameplay that does help but, as I said in the question, I'm trying to avoid iterating over all the property/keys. I was hoping for something on `requests` level, that I could, I don't know, cast to a type that could do the work for me, or a serialisation config that could help. Something that avoids enumeration, as I have a huge load to handle. – Eduardo Elias Saléh Mar 17 '23 at 11:27
  • Not sure I understand to be honest. You said you have 2 responses (not requests). If you can do something on request level I'd just do it. I mean unify before sending. Otherwise after you've received responses you have to iterate or if the format is well defined you can use e.g. pydantic for serialization (define models and use `to_json` to get the desired output. But the format doesn't seem to be well defined if in "propertyContext" there can be literally anything. When that's the case it looks to me like recursive function can work...which takes time on huge data. Maybe someone else can help – Gameplay Mar 17 '23 at 11:37
  • 1
    Technically, under the hood, ANY tool that could do such a conversion would have to iterate over all the items, wouldn't it? Exception might be string (or regex) replacement on the raw data before json parsing... but even that would have to "iterate" until all occurences of "property_type" are replaced with "propertyType", and so on... Anyway, since this not a direct concern for `requests`, I don't think there's anything like it built in. – Jeronimo Mar 17 '23 at 12:56

0 Answers0