Json is below. I need to convert to list after loading from the json. I am getting as str
{
"1": {
"Data (L)": "[ { \"S\" : \"168\" }]"
},
"2": {
"Data (L)": "[ { \"S\" : \"169\" }]"
}
}
Code
with open('a.json') as f:
a = json.loads(f.read())
print (a)
data1 = []
for i,j in a.items():
data = {}
data['id'] = i
data1.append(data)
for k,l in j.items():
data[k] = l
print (type(l))
print (l)
My type pf l is <class 'str'> where output of l is [ { "S" : "168" }]
Expected out
print type(l) has to be list not str
I'm assuming, you have double-encoded json. Just call another json.loads() on values of second dictionaries:
import json
json_text = r'''{
"1": {
"Data (L)": "[ { \"S\" : \"168\" }]"
},
"2": {
"Data (L)": "[ { \"S\" : \"169\" }]"
}
}'''
data = json.loads(json_text)
for k, v in data.items():
for kk, vv in v.items():
vv = json.loads(vv)
print(type(vv))
print(vv)
Prints:
<class 'list'>
[{'S': '168'}]
<class 'list'>
[{'S': '169'}]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With