0

I have this funky code in python.

@route('lda', methods=["POST"])
def lda(self):
    domain = request.json.get("domain", None)
    username = request.json.get("username", None)
    password = request.json.get("password", None)
    server_address = request.json.get("server_address", None)

This of course is not good. How can I deserialize the request.json into an object so that I don't need to type the variables lame like that.

requestData = toObject(request.json)

and then I can use it like this:

xcv = requestData.username ...
realPro
  • 1,713
  • 3
  • 22
  • 34
  • Does this answer your question? [Convert nested Python dict to object?](https://stackoverflow.com/questions/1305532/convert-nested-python-dict-to-object) – tevemadar Jun 11 '22 at 15:08
  • There's also https://stackoverflow.com/questions/4984647/accessing-dict-keys-like-an-attribute and https://stackoverflow.com/questions/3031219/recursively-access-dict-via-attributes-as-well-as-index-access – tevemadar Jun 11 '22 at 15:08
  • @tevemadar No, I need a normal solution. – realPro Jun 11 '22 at 15:20
  • `request.json` (or the result of `request.get_json()`) is a dictionary. The `,None` part is not needed, default value of `get()` is `None` already. Then if you are fine with getting an exception if a key is not present, you could write `["domain"]` and the like instead of `get()`. – tevemadar Jun 11 '22 at 18:07

0 Answers0