Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to extract a single value from JSON response (with Python)?

Hi I’m working on a project with a colleague of mine but we stopped.
We can’t extract a single element of a JSON response.
We have to extrapolate the "mood" attribute from the code that I am attaching, thanks to all in advance:

{
    u'operation_id': u'5526bd03b2e448ea8d2f3e96ae5ba88c', 
    u'photos': [
    {
        u'height': 280,
        u'pid': u'F@01aa37a755638d436b6d527a18da96bc_ffc4e38fc72b2',
        u'tags': [
        {
            u'attributes':
            {
                u'age_est':
                {
                    u'confidence': 50,
                    u'value': u'27'
                },
                u'anger':
                {
                    u'confidence': 0,
                    u'value': u'false'
                },
                u'beard':
                {
                    u'confidence': 91,
                    u'value': u'true'
                },
                u'dark_glasses':
                {
                    u'confidence': 48,
                    u'value': u'false'
                },
                u'disgust':
                {
                    u'confidence': 0,
                    u'value': u'false'
                },
                u'ethnicity':
                {
                    u'asian':
                    {
                        u'confidence': 7,
                        u'value': u'false'
                    },
                    u'black':
                    {
                        u'confidence': 2,
                        u'value': u'false'
                    },
                    u'hispanic':
                    {
                        u'confidence': 3,
                        u'value': u'false'
                    },
                    u'indian':
                    {
                        u'confidence': 3,
                        u'value': u'false'
                    },
                    u'white':
                    {
                        u'confidence': 85,
                        u'value': u'true'
                    }
                },
                u'eyes':
                {
                    u'confidence': 30,
                    u'value': u'open'
                },
                u'face':
                {
                    u'confidence': 95,
                    u'value': u'true'
                },
                u'fear':
                {
                    u'confidence': 0,
                    u'value': u'false'
                },
                u'gender':
                {
                    u'confidence': 83,
                    u'value': u'male'
                },
                u'glasses':
                {
                    u'confidence': 26,
                    u'value': u'false'
                },
                u'happiness':
                {
                    u'confidence': 100,
                    u'value': u'true'
                },
                u'hat':
                {
                    u'confidence': 29,
                    u'value': u'false'
                },
                u'lips':
                {
                    u'confidence': 53,
                    u'value': u'parted'
                },
                u'liveness':
                {
                    u'confidence': 66,
                    u'value': u'true'
                },
                u'mood':
                {
                    u'confidence': 100,
                    u'value': u'happy'
                },
                u'neutral_mood':
                {
                    u'confidence': 0,
                    u'value': u'false'
                },
                u'sadness':
                {
                    u'confidence': 0,
                    u'value': u'false'
                },
                u'smiling':
                {
                    u'confidence': 84,
                    u'value': u'true'
                },
                u'surprise':
                {
                    u'confidence': 0,
                    u'value': u'false'
                }
            },
            u'center':
            {
                u'x': 40.15,
                u'y': 38.93
            },
            u'confirmed': False,
            u'eye_left':
            {
                u'confidence': 98,
                u'id': 449,
                u'x': 44.89,
                u'y': 34.29
            },
            u'eye_right':
            {
                u'confidence': 98,
                u'id': 450,
                u'x': 36.16,
                u'y': 33.93
            },
            u'height': 39.64,
            u'label': None,
            u'manual': False,
            u'mouth_center':
            {
                u'confidence': 91,
                u'id': 615,
                u'x': 41.15,
                u'y': 47.14
            },
            u'nose':
            {
                u'confidence': 99,
                u'id': 403,
                u'x': 41.4,
                u'y': 40.71
            },
            u'pitch': -3,
            u'points': None,
            u'recognizable': True,
            u'similarities': None,
            u'tid': u'TEMP_F@01aa37a755638d436b6d527a00a1006d_ffc4e38fc72b2_40.15_38.93_0_1',
            u'uids': [],
            u'width': 19.7,
            u'yaw': -4
        }],
        u'url': u'http://api.skybiometry.com/fc/images/get?id=bmN2X3hybD1xd3RhY2FvcXJnYzFpMXdkMjIwNGZ4YXB6dCZuY3ZfZnJwZXJnPXZiM3liOXIyOXlmZ25lNjB0bjdvdzlycHRvJmVxPTI3NTMmY3ZxPXNzcDRyMzhzcDcybzImZ3Z6cmZnbnpjPTIwMjExMjE1MTUxNjMz',
        u'width': 401
    }], u'status': u'success', u'usage':
    {
        u'limit': 100,
        u'remaining': 80,
        u'reset_time': 1639553771,
        u'reset_time_text': u'Wed, 15 December 2021 07:36:11 +0000',
        u'used': 20
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Pretty-printing your data makes it a lot easier to analyze. Once you do that, as in my edit above, you’ll see that you can access "mood" via data["photos"][0]["tags"][0]["attributes"]["mood"], assuming your dict is named data.

You need the numeric list indices because both "photos" and "tags" are lists containing a single element – a dict.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading