Stack Overflow Asked by Kay on August 30, 2020
I have an object i stringyfied in JavaScript and i want to parse it in python. However i get the following error:
Traceback (most recent call last): File "main.py", line 7, in
y = json.loads(x) File "/usr/lib/python3.8/json/init.py", line 357, in loads
return _default_decoder.decode(s) File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python3.8/json/decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx) json.decoder.JSONDecodeError: Expecting ‘,’ delimiter: line 1 column 18 (char 17)
javascript
const text = 'K said "Hello World" ';
const obj = {
text: text
}
const stringified = JSON.stringify(obj);
output of stringified is : {"text":"K said "Hello World" "}
python
import json
x = '{"text":"K said "Hello World" "}'
y = json.loads(x)
print(y)
You need a raw string in order to jsonify this as the string is not properly parsed in python. Putting a r before the string should do the work
import json
x = r'{"text":"K said "Hello World" "}'
y = json.loads(x)
print(y)
>>> {'text': 'K said "Hello World" '}
Correct answer by Poojan on August 30, 2020
Try this:
The variable x
has to be
x = "{"text":"K said "Hello World" "}"
y = json.loads(x)
print(y)
>>> {'text': 'K said "Hello World" '}
Answered by Nicolas Parra on August 30, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP