0

I have a script to get data from an URL and transform into a JSON file. I have two items that I'm interested.

This is an example of what they return:

"images": [
        {
            "type": "PosterPortrait", 
            "url": "https://ingresso-a.akamaihd.net/img/cinema/cartaz/22454-cartaz.jpg"
        }, 
        {
            "type": "PosterHorizontal", 
            "url": "https://ingresso-a.akamaihd.net/img/cinema/cartaz/22454-destaque.jpg"
        }
    ], 

"trailers": [
        {
            "embeddedUrl": "//www.youtube.com/embed/YUBBkz5ZbKY", 
            "type": "Youtube", 
            "url": "https://www.youtube.com/watch?v=YUBBkz5ZbKY"
        }, 
        {
            "embeddedUrl": "//www.youtube.com/embed/YUBBkz5ZbKY", 
            "type": "Youtube", 
            "url": "https://www.youtube.com/watch?v=YUBBkz5ZbKY"
        }
    ], 

I need to get the "url" and "type" from each object to save into a Postgresql database OneToMany - movie(One):media(Many). The problem is that "trailers" could be empty and I don't need to save it, since there isn't any data.

code.py

if(i['trailers']):
    a = [
        {'url': i['images'][0]['url'], 'type': i['images'][0]['type']},
        {'url': i['images'][1]['url'], 'type': i['images'][1]['type']},
        {'url': i['trailers'][0]['url'], 'type': 'Trailer'},
        {'url': i['trailers'][1]['url'], 'type': 'Trailer'},
    ] 
else:
    a = [
        {'url': i['images'][0]['url'], 'type': i['images'][0]['type']},
        {'url': i['images'][1]['url'], 'type': i['images'][1]['type']},
    ]

Here is my code. I'm trying to check if there's any elements inside of i['trailers']. If so, he'll be stored inside a dictionary.

Someone could help me to check this, please? Thanks!

4
  • 1
    Not really following fully, but use if (i.get('trailers')), which won't throw an error on a missing key; it will instead return None, which is falsey. Commented Dec 27, 2018 at 22:42
  • @roganjosh TypeError: 'builtin_function_or_method' object is not subscriptable Commented Dec 27, 2018 at 22:45
  • Wut...? :P either you misimplemented my suggestion, you have trampled some built-in or I'm overly tired. Please show the full traceback. Commented Dec 27, 2018 at 22:47
  • What is the type of i in your case? Is it a dictionary? Commented Dec 27, 2018 at 22:48

2 Answers 2

2

If trailers returns an empty list, and images is always not empty, you can replace all your code with one line using a list comprehension:

a = [{'url': x['url'], 'type': x['type']} for x in i['images'] + i['trailers']]

If trailers may be missing or None (instead of an empty list), you just add this line before it:

i['trailers'] = i.get('trailers') or []
Sign up to request clarification or add additional context in comments.

10 Comments

i['trailers'] = i.get('trailers', [])
@roganjosh that won't work because if trailers exists but if None, it'll be None.
I don't know what you mean
@roganjosh if the payload contained {'trailers': None} your way would set it at None instead of an empty list.
What? Have you tested your code vs mine? You're still relying on falsey.
|
1

You can user get method in the Python dictionary as

val = i.get('trailers', None)

This will return i['trailers'] if it exists else it will return None.

You can use this in the if condition.

2 Comments

The None is pointless, that's the default value returned on a failed lookup. You only need the second argument if you want to override the default.
Yea, you are right. You can use it without None as well.

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.