5

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 Gender value from a JSON of face : The JSON is as follows:

{
  "face": [
    {
      "attribute": {
        "age": {
          "range": 5,
          "value": 24
        },
        "gender": {
          "confidence": 99.9999,
          "value": "Female"
        },
        "glass": {
          "confidence": 99.4157,
          "value": "None"
        },
        "pose": {
          "pitch_angle": {
            "value": 0.000001
          },
          "roll_angle": {
            "value": 0.650337
          },
          "yaw_angle": {
            "value": -0.42409
          }
        },
        "race": {
          "confidence": 98.058,
          "value": "Asian"
        },
        "smiling": {
          "value": 3.78394
        }
      },
      "face_id": "42245f24335ad21ea7c54f2db96a09b3",
      "position": {
        "center": {
          "x": 50.121951,
          "y": 35.97561
        },
        "eye_left": {
          "x": 43.465122,
          "y": 30.670488
        },
        "eye_right": {
          "x": 56.80878,
          "y": 30.821951
        },
        "height": 27.560976,
        "mouth_left": {
          "x": 45.649512,
          "y": 45.041707
        },
        "mouth_right": {
          "x": 55.134878,
          "y": 44.858049
        },
        "nose": {
          "x": 50.183415,
          "y": 38.410732
        },
        "width": 27.560976
      },
      "tag": ""
    }
  ],
  "img_height": 410,
  "img_id": "1e3007cb3d6cfbaed3a1b4135524ed25",
  "img_width": 410,
  "session_id": "76ec7f99a471418fa8862a2138cc589d",
  "url": "http://www.faceplusplus.com/wp-content/themes/faceplusplus/assets/img/demo/1.jpg?v=2"
}

I want to extract 'Female' from the above json. And for that, I used this :

 import urllib2
 import io, json
 from bs4 import BeautifulSoup
 data = soup #soup has all the data json
 with open('data.json', 'w') as outfile:
     json.dump(data, outfile, sort_keys = True, indent = 4, ensure_ascii=False)
 #content = json.loads(soup)
 jsonFile = open('data.json', 'r')
 values = json.load(jsonFile)
 jsonFile.close()
 gender = soup['face'][0]['gender']['value']
 print gender

Where is my code incorrect?

3
  • what error or problem is this code causing? Meaning is it giving a syntax error or printing nothing or etc... Commented Jul 4, 2015 at 15:21
  • 1
    as a side note, if your json could be more complicated than that, having a query support can make your life much easier: pypi.python.org/pypi/jq Commented Jul 4, 2015 at 15:52
  • I downloaded the package. But I dont know how to correct the error I am getting. It's giving the error! I tried saving the json in a variable as well as a file... Commented Jul 4, 2015 at 18:48

3 Answers 3

4

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.

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

10 Comments

I tried this also, it gives an error, that TypeError: string indices must be integers
and also maybe print out the value of soup just before accessing this and update that
Traceback (most recent call last): File "ml.py", line 15, in <module> gender = str(soup['face'][0]['attribute']['gender']['value']) TypeError: string indices must be integers This was the complete traceback. soup is exactly same as json
I also tried replacing soup with values. Both give the same error.
And the print of the soup?
|
1

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']

Comments

0

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.

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.