Skip to content Skip to sidebar Skip to footer

How To Get A Fields Particular Value Of JSON In Python?

I am currently working on extracting fields from json and then make some use of that. Hence I have face parameters, and I want to store each field's value. I am trying to fetch the

Solution 1:

According to your json example , gender is inside attribute , so you need to access it as -

gender = soup['face'][0]['attribute']['gender']['value']

Also, seems like values is the json that is read back (dictionary), so you may want to access it using values , though I am not sure what you are trying to achieve so I cannot say for sure.


Solution 2:

Finally, I got an answer and it is working perfect.

with open('data.json') as da:
    data = json.loads(json.load(da))
print data['face'][0]['attribute']['gender']['value']

Solution 3:

You can use some libraries like objectpath, it makes you able to search in JSON in easy way. just import the library and build the object tree, then type your word that you want to search for.

Importing:

import json
import objectpath

Building the search tree:

gender_tree = objectpath.Tree(values['face'])

Typing your searching word

gender_tuple = tuple(actions_tree.execute('$..gender'))

Now, you can deal with gender_tuple for your required values.

Here, the word that you are searching for is "gender", replace it with your suitable word for future searches.


Post a Comment for "How To Get A Fields Particular Value Of JSON In Python?"