0

I am quite new to python , and am struggling to make this work.

I need to convert

["ladksa dlskdlsd slkd sldks lskds lskds","skkdsdl skdsl lskds lsdk sldk sdslkds"] - String Representation of Array To Actual Array Like this.

[0]-> "ladksa dlskdlsd slkd sldks lskds lskds"
[1]-> "skkdsdl skdsl lskds lsdk sldk sdslkds"

I have tried following things:

json.load(array) -> but it gave me parse array error
literal_eval(x)  -> read it somewhere (dont know why it doesn't work)

Error:

custom_classes = json.loads(element.custom_classes)
 File "C:\Users\Shri\AppData\Local\Programs\Python\Python38-32\lib\json\__init_
.py", line 357, in loads
   return _default_decoder.decode(s)
 File "C:\Users\Shri\AppData\Local\Programs\Python\Python38-32\lib\json\decoder
py", line 337, in decode
   obj, end = self.raw_decode(s, idx=_w(s, 0).end())
 File "C:\Users\Shri\AppData\Local\Programs\Python\Python38-32\lib\json\decoder
py", line 355, in raw_decode
   raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 2 (char 1)

Also, I am using below js code to send data:

'custom_classes':'\''+JSON.stringify(finalized_classes)+'\'',

Data Is Stored as : Please check image here

and

arr_element = array of Objects For Specific Class

which has a property

custom_classes -> which is stored as above image.

and i am doing:

for element in arr_element:
    string_obj = json.loads(str(element.custom_classes))
    //here it is error

Please help

3
  • 1
    What error do you get when you try json.load()? Or literal_eval()? You need to post your errors so we can help you. Commented Feb 4, 2021 at 15:31
  • 1
    Use json.loads instead of json.load Commented Feb 4, 2021 at 15:34
  • The last line of your error is missing Commented Feb 4, 2021 at 15:35

2 Answers 2

3
import json

s = '["ladksa dlskdlsd slkd sldks lskds lskds","skkdsdl skdsl lskds lsdk sldk sdslkds"]'

json.loads(s)

# gives ['ladksa dlskdlsd slkd sldks lskds lskds', 'skkdsdl skdsl lskds lsdk sldk sdslkds']
Sign up to request clarification or add additional context in comments.

4 Comments

your codes does work. but i am still facing issue when i am retrieving it from model
Ok, please add to question what exactly is at element.custom_classes.
Hi, i have added info in question.
Thanks, I rather meant to print like for example print('element.custom_classes') and maybe type(element.custom_classes).
0

After you use json.loads(), you can use str.split() to turn the strings into arrays, and then append one of the arrays onto the other if you need to.

foo = ['this is a sentence', 'this is a different sentence']

bar = foo[0].split(' ')
biz = foo[1].split(' ')

bar.append(biz)

print(bar)

Result: ['this', 'is', 'a', 'sentence', 'this', 'is', 'a', 'different', 'sentence']

Comments

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.