Converting Nested Query string requests to a dictionary
I'm experiencing some difficulties converting a querystring data to a well formed dictionary in my view.
Here's my view
class VendorPayloadLayerView(generics.GenericAPIView):
permission_classes = (permissions.AllowAny,)
def get(self, request, *args, **kwargs):
print("Here's the request *****")
print(request)
payload = request.GET
print("Here's the decoded queryDict data")
print(payload)
data = payload.dict()
print("Here's the dictionary")
print(data)
Here is the request to the view:
<rest_framework.request.Request: GET '/turnalerts/api/v2/vendor?%7B%22_vnd%22:%20%7B%22v1%22:%20%7B%22author%22:%20%7B%22id%22:%20%22d2e805b5-4a25-4102-a629-e6b67c798ad6%22,%20%22name%22:%20%22WhatsApp%20Business%20Cloud%20API%22,%20%22request_id%22:%20%22GBV3LTlUEUtfjuMHaDYi%22,%20%22type%22:%20%22SYSTEM%22%7D,%20%22card_uuid%22:%20null,%20%22chat%22:%20%7B%22assigned_to%22:%20%7B%22id%22:%20%2278c711b6-2673-cd8b-0fd9-9a6f03bbcdc5%22,%20%22name%22:%20%22Chima%20Chinda%22,%20%22type%22:%20%22OPERATOR%22%7D,%20%22contact_uuid%22:%20%225ba732cf-d424-4163-9d73-98680d4f53f9%22,%20%22inserted_at%22:%20%222022-05-10T10:15:38.808899Z%22,%20%22owner%22:%20%22%202349039756628%22,%20%22permalink%22:%20%22https://whatsapp-praekelt-cloud.turn.io/app/c/ebd12728-e787-4f29-b938-1059b67f4abd%22,%20%22state%22:%20%22OPEN%22,%20%22state_reason%22:%20%22Re-opened%20by%20inbound%20message.%22,%20%22unread_count%22:%2018,%20%22updated_at%22:%20%222024-12-28T22:17:48.825870Z%22,%20%22uuid%22:%20%22ebd12728-e787-4f29-b938-1059b67f4abd%22%7D,%20%22direction%22:%20%22outbound%22,%20%22faq_uuid%22:%20null,%20%22in_reply_to%22:%20null,%20%22inserted_at%22:%20%222024-12-28T22:17:48.817259Z%22,%20%22labels%22:%20[],%20%22last_status%22:%20null,%20%22last_status_timestamp%22:%20null,%20%22on_fallback_channel%22:%20false,%20%22rendered_content%22:%20null,%20%22uuid%22:%20%227d5fc64e-fd77-325f-8a50-6475e4496775%22%7D%7D,%20%22from%22:%20%2227726968450%22,%20%22id%22:%20%22wamid.HBgNMjM0OTAzOTc1NjYyOBUCABEYEjdCMTJFNUZDNzNFQjkxQ0IyRQA%27:%20%27%22,%20%22preview_url%22:%20false,%20%22recipient_type%22:%20%22individual%22,%20%22text%22:%20%7B%22body%22:%20%22The%20MomConnect%20ADA%20Symptom%20Checker%20is%20unfortunately%20no%20longer%20available.%20%5C%5Cn%5C%5CnPlease%20reply%20*ASK*%20if%20you%20have%20questions%20or%20need%20help.%22%7D,%20%22timestamp%22:%20%221735424268%22,%20%22to%22:%20%222349039756628%22,%20%22type%22:%20%22text%22%7D'>
Here's the decoded queryDict with request.GET
<QueryDict: {'{"_vnd": {"v1": {"author": {"id": "d2e805b5-4a25-4102-a629-e6b67c798ad6", "name": "WhatsApp Business Cloud API", "request_id": "GBV3LTlUEUtfjuMHaDYi", "type": "SYSTEM"}, "card_uuid": null, "chat": {"assigned_to": {"id": "78c711b6-2673-cd8b-0fd9-9a6f03bbcdc5", "name": "Chima Chinda", "type": "OPERATOR"}, "contact_uuid": "5ba732cf-d424-4163-9d73-98680d4f53f9", "inserted_at": "2022-05-10T10:15:38.808899Z", "owner": " 2349039756628", "permalink": "https://whatsapp-praekelt-cloud.turn.io/app/c/ebd12728-e787-4f29-b938-1059b67f4abd", "state": "OPEN", "state_reason": "Re-opened by inbound message.", "unread_count": 18, "updated_at": "2024-12-28T22:17:48.825870Z", "uuid": "ebd12728-e787-4f29-b938-1059b67f4abd"}, "direction": "outbound", "faq_uuid": null, "in_reply_to": null, "inserted_at": "2024-12-28T22:17:48.817259Z", "labels": [], "last_status": null, "last_status_timestamp": null, "on_fallback_channel": false, "rendered_content": null, "uuid": "7d5fc64e-fd77-325f-8a50-6475e4496775"}}, "from": "27726968450", "id": "wamid.HBgNMjM0OTAzOTc1NjYyOBUCABEYEjdCMTJFNUZDNzNFQjkxQ0IyRQA\': \'", "preview_url": false, "recipient_type": "individual", "text": {"body": "The MomConnect ADA Symptom Checker is unfortunately no longer available. \\\\n\\\\nPlease reply *ASK* if you have questions or need help."}, "timestamp": "1735424268", "to": "2349039756628", "type": "text"}': ['']}>
Lastly, here's the dictionary as payload.dict()
Here's the dictionary
{'{"_vnd": {"v1": {"author": {"id": "d2e805b5-4a25-4102-a629-e6b67c798ad6", "name": "WhatsApp Business Cloud API", "request_id": "GBV3LTlUEUtfjuMHaDYi", "type": "SYSTEM"}, "card_uuid": null, "chat": {"assigned_to": {"id": "78c711b6-2673-cd8b-0fd9-9a6f03bbcdc5", "name": "Chima Chinda", "type": "OPERATOR"}, "contact_uuid": "5ba732cf-d424-4163-9d73-98680d4f53f9", "inserted_at": "2022-05-10T10:15:38.808899Z", "owner": " 2349039756628", "permalink": "https://whatsapp-praekelt-cloud.turn.io/app/c/ebd12728-e787-4f29-b938-1059b67f4abd", "state": "OPEN", "state_reason": "Re-opened by inbound message.", "unread_count": 18, "updated_at": "2024-12-28T22:17:48.825870Z", "uuid": "ebd12728-e787-4f29-b938-1059b67f4abd"}, "direction": "outbound", "faq_uuid": null, "in_reply_to": null, "inserted_at": "2024-12-28T22:17:48.817259Z", "labels": [], "last_status": null, "last_status_timestamp": null, "on_fallback_channel": false, "rendered_content": null, "uuid": "7d5fc64e-fd77-325f-8a50-6475e4496775"}}, "from": "27726968450", "id": "wamid.HBgNMjM0OTAzOTc1NjYyOBUCABEYEjdCMTJFNUZDNzNFQjkxQ0IyRQA\': \'", "preview_url": false, "recipient_type": "individual", "text": {"body": "The MomConnect ADA Symptom Checker is unfortunately no longer available. \\\\n\\\\nPlease reply *ASK* if you have questions or need help."}, "timestamp": "1735424268", "to": "2349039756628", "type": "text"}': ''}
The problem here is that the final result is not a valid json as it has an extra brace with an single quote surrounding the dictionary.
What I'm trying to get:
{"_vnd": {"v1": {"author": {"id": "d2e805b5-4a25-4102-a629-e6b67c798ad6", "name": "WhatsApp Business Cloud API", "request_id": "GBV3LTlUEUtfjuMHaDYi", "type": "SYSTEM"}, "card_uuid": null, "chat": {"assigned_to": {"id": "78c711b6-2673-cd8b-0fd9-9a6f03bbcdc5", "name": "Chima Chinda", "type": "OPERATOR"}, "contact_uuid": "5ba732cf-d424-4163-9d73-98680d4f53f9", "inserted_at": "2022-05-10T10:15:38.808899Z", "owner": " 2349039756628", "permalink": "https://whatsapp-praekelt-cloud.turn.io/app/c/ebd12728-e787-4f29-b938-1059b67f4abd", "state": "OPEN", "state_reason": "Re-opened by inbound message.", "unread_count": 18, "updated_at": "2024-12-28T22:17:48.825870Z", "uuid": "ebd12728-e787-4f29-b938-1059b67f4abd"}, "direction": "outbound", "faq_uuid": null, "in_reply_to": null, "inserted_at": "2024-12-28T22:17:48.817259Z", "labels": [], "last_status": null, "last_status_timestamp": null, "on_fallback_channel": false, "rendered_content": null, "uuid": "7d5fc64e-fd77-325f-8a50-6475e4496775"}}, "from": "27726968450", "id": "wamid.HBgNMjM0OTAzOTc1NjYyOBUCABEYEjdCMTJFNUZDNzNFQjkxQ0IyRQA\': \'", "preview_url": false, "recipient_type": "individual", "text": {"body": "The MomConnect ADA Symptom Checker is unfortunately no longer available. \\\\n\\\\nPlease reply *ASK* if you have questions or need help."}, "timestamp": "1735424268", "to": "2349039756628", "type": "text"}
The extra characters (brace and single comma at the start and end of the data) do not appear in the query string so it's not clear to me what's creating it.
Thanks.
That is because a the querystring is just a "one level" dictionary: it makes keys which are strings, to values which are strings. The fact that the value looks like a JSON blob, does not make much sense.
Here you even make it worse because your use one key in the querystring, that maps to no value. The key here is a JSON blob which is ugly. You can get this working by trying to parse the keys into a JSON blob, like:
import json
result = {}
for ky in request.GET:
try:
ky = json.loads(ky)
if isinstance(ky, dict):
result.update(ky)
except ValueError:
pass
But still it is ugly. If you pass JSON blobs as QueryString, do so for a fixed key, and then JSON decode that specific key. Here you are essentially abusing the querystring part of a URL.