Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
62 views
in Technique[技术] by (71.8m points)

How to get an element from a JSON with Python

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Based on the description and information given, this should be your solution:

import json

with open("zalando_it_zalando_it_20190620101450.jl", "r") as jsondata:
    data = json.load(jsondata)

    colors = data["colors"]
    for colorKey in data["stocks"]:
        if colorKey in colors:
            print(colorKey)

We can load the json data directly from the file using json.load, which stores the json as a dictionary to the designated variable (In this code, data).

Then we get each color by getting the key to each stock using a for loop to iterate through all the keys in the dictionary provided by the data["stocks"]. Lastly, we check if the color from the currently checked stock exists in the list of valid colors in data["colors"]. If it is, we print the color.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...