skip to Main Content

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


  1. Maybe try just

    print(data['mechanisms']['label'])
    

    also ur json data is bad try this json data instead maybe

    json_str = '{"mechanisms": {"label": "Mechanismen", "type": "MechSections"}}'
    
    Login or Signup to reply.
  2. There is a missing quote in your json. Also your json doesn’t contain list. Following should work:

    import json
    
    json_str = '{"mechanisms": {"label": "Mechanismen", "type": "MechSections"}}'
    data = json.loads(json_str)
    print(data['mechanisms']['label'])
    

    Output:

    Mechanismen
    
    Login or Signup to reply.
  3. There are small errors in the code you have provided. Try this:

    import json
    
    json_str = '{"mechanisms": {"label": "Mechanismen", "type": "MechSections"}}'
    
    data = json.loads(json_str)
    
    print(data['mechanisms']['label'])
    
    Login or Signup to reply.
  4. Basically, it is a nested dictionary like json

    json_data = '{
       mechanisms": {
           "label": "Mechanismen", 
           "type": "MechSections"
       }
    }'
    

    in the case of dictionary or {} you should use key instead of index

    like that

    json_data['parent_key']['child_key']
    
    json_data['mechanisms']['label']
    json_data['mechanisms']['type']
    

    In the case of the list, it should look like this

    {
       mechanisms": [{
           "label": "Mechanismen", 
           "type": "MechSections"
       }]
    }
    

    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 key

    In this case,

    json_data['parent_key'][list_index]['child_key']
    
    json_data['mechanisms'][0]['label']
    json_data['mechanisms'][0]['type']
    

    Now, in your case its works like that

    import json
    
    json_str = '{mechanisms": {"label": "Mechanismen", "type": "MechSections"}}'
    
    data = json.loads(json_str)
    
    print(data['mechanisms']['label']) 
    # As there is no list representation so you can directly access it by giving `child_key`
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search