Reputation: 93
i've stucked, i have a json with symbols like '
in values and syntax with '
and "
Example mix double qoute and single qoutelink
json ={
'key': "val_'_ue",
'secondkey': 'value'
}
With json loads and json dumps i got a str
type not a dict
to iterate, any ideas how i get it fixed?
print(postParams)# = {'csrf-token': "TOKEN_INCLUDES_'_'_symbols", 'param2': 'params2value'}
jsn_dict2 = json.loads(json.dumps(postParams))
print(type(jsn_dict2)) # ERROR HERE why str and not dict
for key, val in jsn_dict2.items():
print("key="+str(key))
Upvotes: 1
Views: 1903
Reputation: 5745
you dont need to dumps()
an already string json data:
jsn_dict = json.loads(json.dumps(res))
should be :
jsn_dict = json.loads(res)
UPDATE
according to comments the data is looks like so:
postParams = "{'csrf-token': \"TOKEN_INCLUDES_'_'_symbols\", 'add-to-your-blog-submit-button': 'add-to-your-blog-submit-button'}"
so i found an library that can help damaged json string like this one:
first run :
pip install demjson
then this code can help you:
from demjson import decode
data = decode(postParams)
data
>>> {'csrf-token': "TOKEN_INCLUDES_'_'_symbols",
'add-to-your-blog-submit-button': 'add-to-your-blog-submit-button'}
Upvotes: 1
Reputation: 46
In your json u have missed the "," comma separation between two keys. The actual structure of the JSON is
json_new ={ 'key': "val_'_ue", 'secondkey': 'value' }
use
json_actual=json.dumps(json_new)
to read,
json_read=json.loads(json_actual)
Upvotes: 0