1

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))
2
  • json.loads converts JSON to Python; json.dumps converts Python to JSON. Using both in a chain does not make sense. Commented Nov 24, 2020 at 11:15
  • From your comments to the answers it isn't clear whether res is actually JSON. Please edit the question to show its contents, or at least enough of it to provide an minimal reproducible example. Commented Nov 24, 2020 at 13:19

2 Answers 2

1

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'}

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

4 Comments

if i remove the dump i got following error, seems then json loads need doublequotes json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1) normaly i woul said I use str.replace("singelqoute", "doubleqoute" bot this is no option , i have singelqoutes in my values
this was only a example this result which produced the error can finde below (outcomment in code above) {'csrf-token': "TOKEN_INCLUDES_'_'_symbols", 'param2': 'params2value'}
try link the problem is there are double qoutes and single qoutes inmix
do you changed something, i could not see any change on the link not sure if you have access to write on repl .it
0

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)

2 Comments

hi ups i forgot the comma in my example if i remove the dump i got following error json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
lol this is exactly what he was trying to do.... he just didnt present his initial json as a string as intended...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.