Home > Net >  How to remove double quote from key value in dictionary
How to remove double quote from key value in dictionary

Time:05-31

I have this payload:

            "cardType": "CHOICE",
            "step": 40,
            "title": {"en-GB": "YOUR REPORT"},
            "description": {"en-GB": ""},
            "options": [
                {"optionId": 0, "text": {"en-GB": "Ask me"}},
                {"optionId": 1, "text": {"en-GB": "Phone a nurse"}},
                {"optionId": 2, "text": {"en-GB": "Download full report"}},
            ],
            "_links": {
                "self": {
                    "method": "GET",
                    "href": "/assessments/898d915e-229f-48f2-9b98-cfd760ba8965",
                },
                "report": {
                    "method": "GET",
                    "href": "/reports/17340f51604cb35bd2c6b7b9b16f3aec",
                },
            },
        }

I then url encode it like so and redirect to a report view:

url = reverse("my-reports")
reverse_url = encodeurl(data, url)

The urlencode output is returned as this:

"/api/v2/ada/reports?cardType=CHOICE&step=40&title="
        "{'en-GB': 'YOUR REPORT'}&description="
        "{'en-GB': ''}&options="
        "[{'optionId': 0, 'text': {'en-GB"
        "': 'Ask me'}}, {'optionId'%"
        "3A 1, 'text': {'en-GB': 'Phone a nurse"
        "'}}, {'optionId': 2, 'text': {"
        "'en-GB': 'Download full report'}}]&_links="
        "{'self': {'method': 'GET', 'href'%"
        "3A '/assessments/898d915e-229f-48f2-9b98-cfd760ba8965"
        "'}, 'report': {'method': 'GET'%"
        "2C 'href': '/reports/"
        "17340f51604cb35bd2c6b7b9b16f3aec'}}"

In my report view I get the payload from the url query string:

class Reports(generics.GenericAPIView):
    permission_classes = (permissions.AllowAny,)

    def get(self, request, *args, **kwargs):
        result = request.GET
        data = result.dict()
        print(data)

Now the problem is that the output of the transferred data which is printed above has quotes for nested keys.

{
    'cardType': 'CHOICE', 
    'step': '40', 'title': "{'en-GB': 'YOUR REPORT'}", 
    'description': "{'en-GB': ''}", 
    'options': "[{'optionId': 0, 
    'text': {'en-GB': 'Ask me'}}, 
    {'optionId': 1, 'text': {'en-GB': 'Phone a nurse'}}, {'optionId': 2, 'text': {'en-GB': 'Download full report'}}]", 
    '_links': "{'self': {'method': 'GET', 'href': '/assessments/898d915e-229f-48f2-9b98-cfd760ba8965'}, 'report': {'method': 'GET', 'href': '/reports/17340f51604cb35bd2c6b7b9b16f3aec'}}"
}

Notice the double quote in the "description", "options" keys etc. Is there a way to remove this or pass this across to the view so that when I unwrap it I have the same data I started with.

Thanks

CodePudding user response:

import urllib.parse
import json

# convert your dict to a string
json_string = json.dumps(your_payload)

# urlencode the json string
encoded = urllib.parse.quote(json_string.encode('utf-8'))

# build the url
url = reverse("my-reports")
reverse_url = f"{url}?payload={encoded}"

Then in your view:

class Reports(generics.GenericAPIView):
    permission_classes = (permissions.AllowAny,)

    def get(self, request, *args, **kwargs):
        payload = request.GET.get('payload')
        
        # do the encoding process in reverse
        urldecoded = urllib.parse.unquote(payload)
        data = json_loads(urldecoded)
        print(data)
  • Related