Home > Blockchain >  json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 415 - how to get the
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 415 - how to get the

Time:07-15

I have wrote a code like below -

>>> text = "ОБЩЕСТВО С ОГРАНИЧЕННОЙ ОТВЕТСТВЕННОСТЬЮ \"АПК\" \"РАССВЕТ\""
>>> text = text.replace('"', '\"').replace("'", "\'")
>>> data = '{"text": "'   str(text)   '"}'
>>> print(data)
{"text": "ОБЩЕСТВО С ОГРАНИЧЕННОЙ ОТВЕТСТВЕННОСТЬЮ "АПК" "РАССВЕТ""}
>>> final_data = json.loads(data)

json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 53 (char 52)

Do we have any other way to make the double quotes parsed in json.loads ? Help me out please.

CodePudding user response:

As suggested in the comments, you can write your JSON object and then use json.dumps() to get the string, but if you really want to manually write the JSON as a string and then load it with json.loads():

data = """{ "text": "%s" }""" % text.replace('\"', '\\"').replace("'", "\\'")
json.loads(data)  # {'text': 'ОБЩЕСТВО С ОГРАНИЧЕННОЙ ОТВЕТСТВЕННОСТЬЮ "АПК" "РАССВЕТ"'}

With json.dumps():

json.dumps({"text": text}, ensure_ascii=False)
# '{"text": "ОБЩЕСТВО С ОГРАНИЧЕННОЙ ОТВЕТСТВЕННОСТЬЮ \\"АПК\\" \\"РАССВЕТ\\""}'
  • Related