AnswerBun.com

How to parse a json in python which was created with json stringfy using javascript

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)

2 Answers

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

Add your own answers!

Related Questions

Copy each cell in a range to multi-cell range

2  Asked on November 29, 2021 by preston-richardson

   

How to make dynamic custom textarea with jquery?

3  Asked on November 29, 2021 by david-noya

     

private static field Java

3  Asked on November 29, 2021

         

unordered_map excess calls to hash function

4  Asked on November 29, 2021 by amir-kirsh

   

cant load static file in django

1  Asked on November 29, 2021

       

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP