I don’t understand why I can’t print out the data from "label". After what I’ve looked up this should do it:
import json
json_str = '{mechanisms": {"label": "Mechanismen", "type": "MechSections"}}'
data = json.loads(json_str)
print(data['mechanisms'][0]['label'])
Instead of giving me the content from "label" I get an error. This is the error:
Traceback (most recent call last):
File "D:/PyCharm/Projekte/Vivia_Medikamente/main.py", line 5, in
data = json.loads(json_str)
File "C:UsersBennettAppDataLocalProgramsPythonPython37libjson_init_.py", line 348, in loads
return _default_decoder.decode(s)
File "C:UsersBennettAppDataLocalProgramsPythonPython37libjsondecoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:UsersBennettAppDataLocalProgramsPythonPython37libjsondecoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
4
Answers
Maybe try just
also ur json data is bad try this json data instead maybe
There is a missing quote in your json. Also your json doesn’t contain list. Following should work:
Output:
There are small errors in the code you have provided. Try this:
Basically, it is a nested dictionary like json
in the case of dictionary or
{}
you should usekey
instead ofindex
like that
In the case of the list, it should look like this
Now for this case, as you see there is
[]
before{}
then you should give the index first then you can access the inner dictionary element by keyIn this case,
Now, in your case its works like that