2

im trying to have this code return type dict from string I tried using

import json

box_of_cookies = "{'domain': '.kith.com', 'expiry': 1662402888, 'httpOnly': False, 'name': 'geolizr_geodata', 'path': '/', 'sameSite': 'Strict', 'secure': True, 'value': '{%22as%22:%22AS701%20MCI%20Communications%20Services%2C%20Inc.%20d/b/a%20Verizon%20Business%22%2C%22asname%22:%22UUNET%22%2C%22mobile%22:false%2C%22proxy%22:false%2C%22city%22:%22East%20Orange%22%2C%22currency%22:{%22code%22:%22USD%22}%2C%22country%22:{%22code%22:%22US%22%2C%22country%22:%22United%20States%22}%2C%22countryCode%22:%22US%22%2C%22continent%22:%22North%20America%22%2C%22continentCode%22:%22NA%22%2C%22isp%22:%22MCI%20Communications%20Services%2C%20Inc.%20d/b/a%20Verizon%20Business%22%2C%22lat%22:40.7523%2C%22lon%22:-74.2172%2C%22org%22:%22Verizon%20Communications%22%2C%22query%22:%2274.102.170.52%22%2C%22region%22:%22NJ%22%2C%22regionName%22:%22New%20Jersey%22%2C%22status%22:%22success%22%2C%22timezone%22:%22America/New_York%22%2C%22zip%22:%2207018%22%2C%22cloudflare%22:%22US%22%2C%22ttl%22:2399%2C%22env%22:%22PROD%22%2C%22version%22:%22ip-api%20cached%22%2C%22currencyCode%22:%22USD%22%2C%22countryName%22:%22United%20States%22%2C%22service%22:%22ip.lovely-app.com%22}'}, {'domain': '.kith.com', 'expiry': 1630868688, 'httpOnly': False, 'name': '_shopify_sa_t', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '2021-09-05T18%3A34%3A48.213Z'}, {'domain': 'kith.com', 'expiry': 1693938888, 'httpOnly': False, 'name': '__kla_id', 'path': '/', 'secure': False, 'value': 'eyIkcmVmZXJyZXIiOnsidHMiOjE2MzA4NjY4ODYsInZhbHVlIjoiIiwiZmlyc3RfcGFnZSI6Imh0dHBzOi8va2l0aC5jb20vYWNjb3VudC9sb2dpbiJ9LCIkbGFzdF9yZWZlcnJlciI6eyJ0cyI6MTYzMDg2Njg4OCwidmFsdWUiOiIiLCJmaXJzdF9wYWdlIjoiaHR0cHM6Ly9raXRoLmNvbS9hY2NvdW50L2xvZ2luIn19'}, {'domain': 'kith.com', 'expiry': 1662402887, 'httpOnly': True, 'name': 'secure_customer_sig', 'path': '/', 'secure': True, 'value': 'e2f7a59a5aec576e527d7240d1db9f7b'}, {'domain': '.kith.com', 'expiry': 1630868688, 'httpOnly': False, 'name': '_shopify_sa_p', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': ''}, {'domain': 'kith.com', 'expiry': 1632076486, 'httpOnly': True, 'name': 'cart_sig', 'path': '/', 'sameSite': 'None', 'secure': True, 'value': '90d14af23f488d1426f499a12b8cfd28'}, {'domain': 'kith.com', 'expiry': 1662402888, 'httpOnly': False, 'name': 'KL_FORMS_MODAL', 'path': '/', 'secure': False, 'value': '{%22disabledForms%22:{}%2C%22viewedForms%22:{%22TFUkCm%22:3408578}}'}, {'domain': '.kith.com', 'expiry': 1630866945, 'httpOnly': False, 'name': '_gat_gtag_UA_62344036_3', 'path': '/', 'secure': False, 'value': '1'}, {'domain': 'kith.com', 'expiry': 1630953287, 'httpOnly': True, 'name': '_secure_session_id', 'path': '/', 'sameSite': 'None', 'secure': True, 'value': '0c1901c20ab9506de47abaa56ee3ee11'}, {'domain': '.kith.com', 'expiry': 1630868688, 'httpOnly': False, 'name': '_shopify_s', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'e6c34f1d-6eb1-4fda-a683-fe9c7428f49e'}, {'domain': 'kith.com', 'expiry': 1630870486, 'httpOnly': False, 'name': 'shopify_pay_redirect', 'path': '/', 'secure': False, 'value': 'pending'}, {'domain': '.kith.com', 'expiry': 1662402887, 'httpOnly': False, 'name': '_y', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'cab0fbd3-25c8-4939-949f-441b5eacb116'}, {'domain': '.kith.com', 'expiry': 1630953288, 'httpOnly': False, 'name': '_gid', 'path': '/', 'secure': False, 'value': 'GA1.2.1460112824.1630866886'}, {'domain': 'kith.com', 'expiry': 1630868687, 'httpOnly': False, 'name': 'dynamic_checkout_shown_on_cart', 'path': '/', 'secure': False, 'value': '1'}, {'domain': '.kith.com', 'expiry': 1662402887, 'httpOnly': False, 'name': '_shopify_y', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'cab0fbd3-25c8-4939-949f-441b5eacb116'}, {'domain': 'kith.com', 'expiry': 1630868684, 'httpOnly': False, 'name': '_shopify_country', 'path': '/', 'secure': False, 'value': 'United+States'}, {'domain': '.kith.com', 'expiry': 1632076484, 'httpOnly': True, 'name': '_landing_page', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '%2Faccount%2Flogin'}, {'domain': 'kith.com', 'httpOnly': False, 'name': 'snize-recommendation', 'path': '/', 'secure': False, 'value': '3s53e71zpk6'}, {'domain': '.kith.com', 'expiry': 1693938886, 'httpOnly': False, 'name': 'GlobalE_CT_Data', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '%7B%22CUID%22%3A%22561504970.682656459.583%22%2C%22CHKCUID%22%3Anull%7D'}, {'domain': '.kith.com', 'expiry': 1630868688, 'httpOnly': False, 'name': '_s', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'e6c34f1d-6eb1-4fda-a683-fe9c7428f49e'}, {'domain': '.kith.com', 'expiry': 1632076484, 'httpOnly': True, 'name': '_orig_referrer', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': ''}, {'domain': '.kith.com', 'expiry': 1662402889, 'httpOnly': False, 'name': '__zlcmid', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '15vkJEQAVa9KC2y'}, {'domain': '.kith.com', 'httpOnly': False, 'name': 'GlobalE_SupportThirdPartCookies', 'path': '/', 'secure': False, 'value': 'true'}, {'domain': '.kith.com', 'expiry': 1631126086, 'httpOnly': False, 'name': 'GlobalE_Data', 'path': '/', 'secure': False, 'value': '%7B%22countryISO%22%3A%22US%22%2C%22currencyCode%22%3A%22USD%22%2C%22cultureCode%22%3A%22en-US%22%7D'}, {'domain': '.kith.com', 'expiry': 1630866945, 'httpOnly': False, 'name': '_gat', 'path': '/', 'secure': False, 'value': '1'}, {'domain': 'kith.com', 'expiry': 1632076487, 'httpOnly': False, 'name': 'cart_currency', 'path': '/', 'secure': False, 'value': 'USD'}, {'domain': '.kith.com', 'httpOnly': False, 'name': 'GlobalE_Full_Redirect', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'false'}, {'domain': '.kith.com', 'expiry': 1693938888, 'httpOnly': False, 'name': '_ga', 'path': '/', 'secure': False, 'value': 'GA1.2.797558024.1630866886'}"


#adds cookie to driver
def parse_Cookie_string(box_of_cookies):
    #print(cookie.split("},"))
    cookies_list = list(box_of_cookies.split("},"))
    for cookie in cookies_list:
        #check if its the last number element in the list if it is dont add the }
        if(cookie == cookies_list[-1]):
            pass
        else:
            cookie += "}" 
        
        
        print("{}\n".format(type(box_of_cookies)))
        #driver.add_cookie(cookie)

ive tried adding putting json.loads(box_of_cookies) but its juts ends up giving me a error saying

Expecting value: line 1 column 59 (char 58)
2
  • its a json gotten from a cookie Commented Sep 5, 2021 at 18:34
  • This json is not formatted according to json standards. Are you sure that it's intended to be in json format? For example, True and False in json format must be lower case: true and false. And all those extra symbols %:{}" are probably throwing the json parser off. This really should not be parsed using json. Unfortunately, I know of nothing it could be parsed with. Commented Sep 5, 2021 at 18:42

1 Answer 1

1

The True and False must be lowercase, and JSON must be one object; the multiple objects must be placed inside an array. To rectify this, place the following code after the string (as an example) to alter the JSON to make it valid. The code is not exhaustive, but will work for these circumstances.

box_of_cookies = box_of_cookies.replace('False', 'false')
box_of_cookies = box_of_cookies.replace('True', 'true')
box_of_cookies = '[' + box_of_cookies + ']'
box_of_cookies = box_of_cookies.replace('\'', '"')

Then, the json.loads() should work as expected.

Sign up to request clarification or add additional context in comments.

5 Comments

good point but not enough... i still got an error. I also apply .replace('None', 'null') but error, why? how you fixed it?
Interesting, json.loads() works fine for me on Python 3.9 after importing json.
also 3.9. Could you make your answer complete with the argument of loads? I tried to load the string with and without the function call parse_Cookie_string but always same error, JSONDecodeError
I got the same result with an online interpreter online-python.com/yWD7TrxFZ0
My mistake, I somehow switched the quotes around in testing. It should work now with the altered code above; it just replaces the single quotes with double quotes to match JSON standards.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.