X = """{"botResponse": "Great question! 🎉 So you want to find out if \( \sqrt{44} \) is closer to 6 or 7. Let's solve this together, step by step! 🕵️♀️
Step 1: Find the squares of 6 and 7. So, \( 6^2 = 36 \) and \( 7^2 = 49 \).
Step 2: Now, observe that 44 is closer to 49 than it is to 36. 🤓
Step 3: So, \( \sqrt{44} \) will be closer to \( \sqrt{49} \) which equals 7. 🎈
Therefore, \( \sqrt{44} \) is closer to 7! 🎉 Hope this helps!",
"quickReplies": ["Could you solve another problem for me? 🙋", "Give me a similar question to practice? 🎯", "Explain this to me using a fun example that's easy to understand 🤓"]}"""
I want to convert this to a proper json
or dict
format. How can I do as I've tried everything?
json.loads(X) # Error with and without strict = True / False
json.loads(repr(X)) # Error
json.loads(re.sub("\\", "SOMETHINGELSE", X)) # Still error
I've done the same above with ast.literal_eval()
and still getting error.
First you will have to get rid of those backslashes, then you will need to enable control characters by using "strict=False". When dumping the dict again, you will have to make sure to set ensure_ascii to False, because your emojis will not be displayed otherwise.
X = X.replace("\\", "\\\\")
x_dict = json.loads(X, strict=False)
print(json.dumps(x_dict, ensure_ascii=False, indent=4))
That produces the following output:
{ "botResponse": "Great question! 🎉 So you want to find out if \( \sqrt{44} \) is closer to 6 or 7. Let's solve this together, step by step! 🕵️♀️\n\nStep 1: Find the squares of 6 and 7. So, \( 6^2 = 36 \) and \( 7^2 = 49 \).\n\nStep 2: Now, observe that 44 is closer to 49 than it is to 36. 🤓\n\nStep 3: So, \( \sqrt{44} \) will be closer to \( \sqrt{49} \) which equals 7. 🎈\n\nTherefore, \( \sqrt{44} \) is closer to 7! 🎉 Hope this helps!", "quickReplies": [ "Could you solve another problem for me? 🙋", "Give me a similar question to practice? 🎯", "Explain this to me using a fun example that's easy to understand 🤓" ] }