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!
if (i.get('trailers')), which won't throw an error on a missing key; it will instead returnNone, which is falsey.TypeError: 'builtin_function_or_method' object is not subscriptableiin your case? Is it a dictionary?