0

I'm having some hard time filtering multiple json datas, I need to know the type of each data and if the type corresponds to a fruit then print the element's fields key, see python example comments for a better explanation.

Here's what the JSON looks like :

#json.items()

{
  'type': 'apple', 
  'fields': {
    'protein': '18g', 
    'glucide': '3%', 
   }
},  
{
  'type': 'banana', 
  'fields': {
    'protein': '22g', 
    'glucide': '8%', 
  }
}, 

Here's what I tried to do :

for key, value in json.items(): #access json dict.
    if key == 'type':           #access 'type' key
        if value == 'apple':        #check the fruit
            if key == 'fields':        #ERROR !!! Now I need to access the 'fields' key datas of this same fruit. !!!
                print('What a good fruit, be careful on quantity!')
                print('more :' + value['protein'] + ', ' + value['glucid'])

        if value == 'banana':    #if not apple check for bananas
            print('One banana each two days keeps you healthy !')
            print('more:' + value['protein'] + ', ' + value['glucid'])

Is there a way I can achieve this ?

5
  • If what you posted is your entire json, then you have a list of dicts, not just a dict. You need 2 loops. Commented Jul 23, 2017 at 21:13
  • @cᴏʟᴅsᴘᴇᴇᴅ What I posted doesn't look anything like my json because it has too much datas, this one is simplified so it's more understandable, but the number of 'fruit' vary Commented Jul 23, 2017 at 21:14
  • So printing out json gives you a list of those dicts? Commented Jul 23, 2017 at 21:20
  • Your 1st and 3rd if will never be true at the same time. Commented Jul 23, 2017 at 21:29
  • @KlausD. Yeah I saw that, that was the reason why I was asking the question Commented Jul 23, 2017 at 21:40

2 Answers 2

1

What you have seems to be a list of dicts.

You then check if keys type and fields exist in the dictionary before checking their value, like this:

for d in data: # d is a dict
    if 'type' in d and 'fields' in d:
        if d['type'] == 'apple':
            ... # some print statements

        elif d['type'] == 'banana':
            ... # some more print statements
Sign up to request clarification or add additional context in comments.

3 Comments

This works ! For usage purpose is it possible to not specify each key in the if statement and loop for all the existing keys ?
@Lindow If you want to print unique statements for each item, I don't believe it is possible.
@Lindow If you have a common print statement and nothing else, you can drop the ifs and just print something like print("{}s have {} of protein and {} glucide".format(d['type'], d['fields']['protein'], d['fields']['glucide']))
0

Based on your representation of the JSON, it appears that is actually a list, not a dictionary. So in order to iterate through it, you could try something like this:

for item in json:
    fields = item['fields']
    if item['type'] == 'banana':
        print('Bananas have {} of protein and {} glucide'.format(fields['protein'], fields['glucide']))
    elif item['type'] == 'apple':
        print('Apples have {} of protein and {} glucide'.format(fields['protein'], fields['glucide']))

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.