I have a JSON with this two lines:
"colors": ["dark denim"]
"stocks": {"dark denim": {"128": 4, "134": 6, "140": 17, "146": 18, "152": 35, "158": 7, "164": 21}}
I have to check if the 'colors' value, in this case 'dark denim', has the same value as in the first field of 'stocks.
The 'colors' always has one value, and the first field in stock also.
This is the whole json line, if it helps:
{"meta": {"upload": "2019-06-20 10:15:14.580562"}, "url": {"it": ["https://www.zalando.it/name-it-nkfmello-jacket-small-dot-giacca-da-mezza-stagione-dark-denim-na823l0c7-k11.html"]}, "product_id": "https://www.zalando.it/name-it-nkfmello-jacket-small-dot-giacca-da-mezza-stagione-dark-denim-na823l0c7-k11.html", "source": "Zalando", "country": ["it"], "lang": "it", "ref": "NA823L0C7-K11", "sex": "girls", "main_title": {"it": "NKFMELLO JACKET SMALL DOT - Giacca da mezza stagione"}, "images": {"dark denim": ["https://mosaic03.ztat.net/vgs/media/pdp-reco-2x/NA/82/3L/0C/7K/11/[email protected]"]}, "price_hierarchy": {"type": "color", "dark denim": {"price": {"EUR": "18.89"}, "previous_price": {"EUR": "26.99"}}}, "details": {"colors": ["dark denim"], "material": "Composizione: 100% poliestere, Fodera: 100% cotone", "brand": "Name it", "sizes": {"dark denim": ["128", "134", "140", "146", "152", "158", "164"]}, "sizes_not_available": {"dark denim": []}, "all_sizes": {"dark denim": ["146", "158", "128", "140", "134", "152", "164"]}}, "description": {"it": "Colletto: Cappuccio, Chiusura: Cerniera, Tasche: Tasche con patta, Cappuccio: Cappuccio removibile, Fantasia: Pois Vestibilità: Normale, Lunghezza: Lunghezza normale, Lunghezza manica: Manica lunga, Lunghezza delle maniche: 49 cm nella taglia 128, Larghezza dello schienale: 32 cm nella taglia 128, Lunghezza totale: 55 cm nella taglia 128 Composizione: 100% poliestere"}, "category": {"it": ["abbigliamento"]}, "sub_category": {"it": ["giacche"]}, "stocks": {"dark denim": {"128": 4, "134": 6, "140": 17, "146": 18, "152": 35, "158": 7, "164": 21}}, "full_path": [["abbigliamento", "giacche"]]}
The code I have so far is:
import json
with open('zalando_it_zalando_it_20190620101450.jl') as json_file:
for line in json_file:
json_line = (json.loads(line))
color_name = json_line['details']['colors']
if json_line['stocks'][0] == color_name:
print(color_name)
So as you can see, I tried access it using [0], but that doesn't work, because one its a list and the other one is a dictionary.
My question is: How can I check if the value of 'colors' is the same as the first field of 'stocks'?
question from:
https://stackoverflow.com/questions/65843594/how-to-get-an-element-from-a-json-with-python