Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
575 views
in Technique[技术] by (71.8m points)

urlencode - json to url encoding for canonical request in python

I am using urllib package. I am trying to encode nested JSON which I am padding from postman as JSON.

For decoding, I am using the site:"https://www.urldecoder.io/"

My code is encoding properly the only issue is adding "uniqueIdentifier" two times. I do not need a second one i.e bold in my result.

Payload:

{"accessId": "438kjhkjh",
"storeDetail": {
    "storeIdType": "shop",
    "storeId": "check"
},
"catalogueDetails": [
    {
        "brand": "brand",
        "uniqueIdentifier": ["uniqueIdentifier"],
        "uniqueIdentifierType": "uniqueIdentifierType"
    }
],
"merchantId": "T123T",
"transactionTimeout": "900",
"currencyCode": "INR"}

My Code:

from urllib.parse import quote
def format_parameters(params):
    param_string = ''
    updatedV = ''
    updatedV1 = ''
    try:
        for k, v in sorted(params.items()):
            # print("k:: ",k)
            # print("v:: ",v)
            if(k=='storeDetail'):
                for a,b in v.items():
                    updatedV += quote(a) + '=' + quote(str(b)) + ', '
                updatedV = updatedV[:-2]
                updatedV = "{"+updatedV+"}"
                param_string += quote(k) + '=' + quote(str(updatedV)) + '&'
            elif(k=='catalogueDetails'):
              for c,d in v[0].items():
                  if (c=='uniqueIdentifier'):
                    updatedV1 += quote(c) + '=[' + quote(str(d[0])) + '], '

                  updatedV1 += quote(c) + '=' + quote(str(d)) + ', '
              print(updatedV1)
              updatedV1 = updatedV1[:-2]
              updatedV1 = "[{"+updatedV1+"}]"
              param_string += quote(k) + '=' + quote(str(updatedV1)) + '&'
            else:
              param_string += quote(k) + '=' + quote(str(v)) + '&'

    except Exception as ex:
        print (ex)
    param_string = param_string[:-1]
    return param_string.replace('+', '%20').replace('*', '%2A').replace('%7E', '~').replace("%27","")

Encoded output

accessId=438kjhkjh&catalogueDetails=%5B%7Bbrand%3Dbrand%2C%20uniqueIdentifier%3D%5BuniqueIdentifier%5D%2C%20uniqueIdentifier%3D%255B%2527uniqueIdentifier%2527%255D%2C%20uniqueIdentifierType%3DuniqueIdentifierType%7D%5D&currencyCode=INR&merchantId=T123T&storeDetail=%7BstoreIdType%3Dshop%2C%20storeId%3Dcheck%7D&transactionTimeout=900

Decoded output via "https://www.urldecoder.io/"

accessId=438kjhkjh&catalogueDetails=[{brand=brand, uniqueIdentifier=[uniqueIdentifier], uniqueIdentifier=%5B%27uniqueIdentifier%27%5D, uniqueIdentifierType=uniqueIdentifierType}]&currencyCode=INR&merchantId=T123T&storeDetail={storeIdType=shop, storeId=check}&transactionTimeout=900

Note:

It's my requirement that's why I am doing some manual work too, I have tried urllib but that was not as per requirement.

question from:https://stackoverflow.com/questions/65923133/json-to-url-encoding-for-canonical-request-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I added one more "if" condition like this:

if (c=='uniqueIdentifier'):
  updatedV1 += quote(c) + '=[' + quote(str(d[0])) + '], '
if (c!='uniqueIdentifier'):
  updatedV1 += quote(c) + '=' + quote(str(d)) + ', '

Working as expected.

Thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...