1

I have a Json array which has key value pairs. I want to get value of a particular key in the list. I don't know at what position the key will be in the array so I cant use the index in the array.

How can I get this please? I have tried something like below code to get value of 'filterB' which is 'val1' but no luck. Thanks.

import json
x = '{"filters":[{"filterA":"All"},{"filterB":"val1"}]}'
y = json.loads(x)
w = y['filters']['filterB']
print (w)
3
  • How would you do it if the data didn't come from JSON? Commented Jul 28, 2021 at 19:00
  • I would have to iterate it through the list. I wanted to see if it is possible to get the values of Jarray based on key without iterating the whole array list. Commented Jul 28, 2021 at 19:02
  • It's just a list, nothing to do with an "array"; and the way you do it when the data comes from JSON is the same way - because the data that you have is identical to what you'd get if there were no JSON involved. That's the point of the json library: to convert JSON documents into ordinary Python data. Commented Jul 28, 2021 at 19:05

2 Answers 2

2

w = y['filters']['filterB'] doesn't work because y['filters'] is a list and not dict.

The answer to your question depends on how you want to handle the case of multiple dictionaries inside filters list that have filterB key.

import json
x = '{"filters":[{"filterA":"All"},{"filterB":"val1"}]}'
y = json.loads(x)

# all filterB values
filter_b_values = [x['filterB'] for x in y['filters'] if 'filterB' in x.keys()] 

# take first filterB value or None if no values
w = filter_b_values[0] if filter_b_values else None

Sign up to request clarification or add additional context in comments.

2 Comments

You don't need to check 'filterB' in x.keys(). 'filterB' in x does the same thing
@gabip This is a simpler version that I was expecting to implement. Thanks very much.
2

The source of your data (json) has nothing to do with what you want, which is to find the dictionary in y['filters'] that contains a key called filterB. To do this, you need to iterate over the list and look for the item that fulfils this condition.

w = None
for item in y['filters']:
    if 'filterB' in item:
        w = item['filterB']
        break

print(w) # val1

Alternatively, you could join all dictionaries into a single dictionary and use that like you originally tried

all_dict = dict()
for item in y['filters']:
    all_dict.update(item)

# Replace the list of dicts with the dict
y['filters'] = all_dict

w = y['filters']['filterB']
print(w) # val1

If you have multiple dictionaries in the list that fulfil this condition and you want w to be a list of all these values, you could do:

y = {"filters":[{"filterA":"All"},{"filterB":"val1"},{"filterB":"val2"}]}
all_w = list()
for item in y['filters']:
    if 'filterB' in item:
        all_w.append(item['filterB'])

Or, as a list-comprehension:

all_w = [item['filterB'] for item in y['filters'] if 'filterB' in item]
print(all_w) # ['val1', 'val2']

Note that a list comprehension is just syntactic sugar for an iteration that creates a list. You aren't avoiding any looping by writing a regular loop as a list comprehension

2 Comments

Thank you. Is it possible to achieve this without iteration? I tried something like below but it is not working. l =['filterB'] z = [w[x] for x in l if x in w] print(z)
@Muthu No. Since you have a list, you must iterate over each item. Even if you use a different function or a list comprehension, there is iteration hidden at some level

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.