0

Please take a look at this post for more info I am trying to find something like this

screen_shots = full_data['tabs'][0]['views'][1]['screenshots']

but for a new JSON file but I keep getting

KeyError: 'screenshots'

I tried a few things to fix it but nothing works so any help will be welcomed

JSON

    {
"minVersion":"0.1",
"class":"DepictionTabView",
"tintColor":"#2cb1be",
"headerImage":"",
"tabs":[ {
    "tabname":"Details",
    "class":"DepictionStackView",
    "tintColor":"#2cb1be",
    "views":[ {
        "class": "DepictionSubheaderView", "useBoldText": true, "useBottomMargin": false, "title": "Description"
    }
    ,
    {
        "class": "DepictionMarkdownView", "markdown": "<p>This is a description...<\/p>", "useRawFormat": true
    }
    ,
    {
        "class": "DepictionSeparatorView"
    }
    ,
    {
        "class": "DepictionSubheaderView", "useBoldText": true, "useBottomMargin": false, "title": "Screenshots"
    }
    ,
    {
        "class":"DepictionScreenshotsView",
        "itemCornerRadius":6,
        "itemSize":"{160, 284.44444444444}",
        "screenshots":[ {
            "accessibilityText": "Screenshot", "url": "Screenshot URL 1"
        }
        ]
    }
    ,
    {
        "class": "DepictionSeparatorView"
    }
    ,
    {
        "class": "DepictionSubheaderView", "useBoldText": true, "useBottomMargin": false, "title": "Information"
    }
    ,
    {
        "class": "DepictionTableTextView", "title": "Author", "text": "User"
    }
    ,
    {
        "class": "DepictionTableTextView", "title": "Version", "text": "1.0"
    }
    ,
    {
        "class": "DepictionTableTextView", "title": "Price", "text": "free"
    }
    ,
    {
        "class": "DepictionSpacerView", "spacing": 16
    }
    ,
    {
        "class":"DepictionStackView",
        "views":[ 
        {
            "class": "DepictionTableButtonView", "title": "Support", "action": "", "openExternal": true
        }
        ]
    }
    ,
    {
        "class": "DepictionSpacerView", "spacing": 16
    }
    ]
}
,
{
    "tabname":"Changelog",
    "class":"DepictionStackView",
    "tintColor":"#2cb1be",
    "views":[ 
    {
        "class": "DepictionSubheaderView", "useBoldText": true, "useBottomMargin": false, "title": "1.0"
    }
    ,
    {
        "class": "DepictionMarkdownView", "markdown": "<ul>\n<li>Initial release.<\/li>\n<\/ul>", "useRawFormat": true
    }
    ]
}
]
}
2
  • There is no screenshots in the second view object. screen_shots = full_data['tabs'][0]['views'][4]['screenshots'] would potentially remove your error. Commented Apr 28, 2019 at 17:43
  • Welcome to StackOverflow! It would be great if you could update your question and explain why you're trying to look for the screenshots key on a view that does not have one. Commented Apr 28, 2019 at 17:45

2 Answers 2

3

In your JSON, many of the views do not have a screenshots key. The 2nd view (views[1]) definitely does not while the 5th view (views[4]) does.

If you're trying to collect all the screenshots from the views you'll need to use a loop and some conditional logic to find them.

screenshots = [] # an accumulator to collect our screenshots
for view in full_data['tabs'][0]['views']: # loop over each view
    if 'screenshots' in view: # only process views with a screenshots key
        # there can be multiple screenshots per view, so concatenate them to our accumulator
        screenshots += view['screenshots']

You can further simplify this to a list comprehension

screenshots = [*screenshot for screenshot in full_data['tabs'][0]['views'] if 'screenshots' in view]

The *screenshot tells Python to "unroll" the list of screenshots. Adding each item to the list, rather then inserting a list into our list.

Sign up to request clarification or add additional context in comments.

Comments

0

In the example you posted:

screen_shots = full_data['tabs'][0]['views'][1]['screenshots']

Goes to:

{
        "class": "DepictionMarkdownView", "markdown": "<p>This is a description...<\/p>", "useRawFormat": true
}

Which doesn't have any key called screenshots. Your JSON keys at that level are not guaranteed to have that key. I'm guessing that screenshots will only be there if class is DepictionScreenshotsView. So you can try:

tab = full_data['tabs'][0]
screen_shots = [tab['views'][i]['screenshots'] for i in range(len(tab)) if tab['view'][i]['class'] == 'DepictionScreenshotsView')

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.