Home > Blockchain >  How to get specific value from JSON response in Python
How to get specific value from JSON response in Python

Time:11-30

I have a response coming in as :

b'
{
    "_items": [
        {
            "_id": "61a8dc29fab70adfacf59789",
            "name": "CP",
            "url": "",
            "sd_subscriber_id": "",
            "account_manager": "",
            "contact_name": "",
            "contact_email": "",
            "phone": "",
            "country": "other",
            "is_enabled": true,
            "company_type": null,
            "monitoring_administrator": null,
            "allowed_ip_list": null,
            "expiry_date": null,
            "original_creator": "6183d49420d13bc4e332281d",
            "events_only": false,
            "_created": "2021-12-02T14:46:01 0000",
            "_updated": "2022-02-06T11:59:32 0000",
            "_etag": "277e2a8667b650fe4ba56f4b9b44780f3992062a",
            "archive_access": false,
            "sections": {
                "wire": true,
                "agenda": true,
                "news_api": true,
                "monitoring": true
            },
            "_links": {
                "self": {
                    "title": "Companie",
                    "href": "companies/61a8dc29fab70adfacf59789"
                },
                "related": {
                    "original_creator": {
                        "title": "User",
                        "href": "users/6183d49420d13bc4e332281d"
                    }
                }
            }
        },
        {
            "_id": "635ac6b9b837aa06e8e94ea3",
            "name": "Load Company No Exp",
            "url": "",
            "sd_subscriber_id": "",
            "account_manager": "",
            "contact_name": "",
            "contact_email": "[email protected]",
            "phone": "6478934734",
            "country": "",
            "is_enabled": true,
            "company_type": null,
            "monitoring_administrator": null,
            "allowed_ip_list": null,
            "expiry_date": null,
            "original_creator": "6298c949007f2fb1c968dfdf",
            "events_only": false,
            "_created": "2022-10-27T17:58:17 0000",
            "_updated": "2022-10-27T18:03:17 0000",
            "_etag": "9cb17d520b3ca9dc1c3326a1ccab8bbb5e7839f2",
            "version_creator": "6183d49420d13bc4e332281d",
            "_links": {
                "self": {
                    "title": "Companie",
                    "href": "companies/635ac6b9b837aa06e8e94ea3"
                },
                "related": {
                    "original_creator": {
                        "title": "User",
                        "href": "users/6298c949007f2fb1c968dfdf"
                    },
                    "version_creator": {
                        "title": "User",
                        "href": "users/6183d49420d13bc4e332281d"
                    }
                }
            }
        }
    ]
}

All i Need is the _items part of it that is inside the [] :

[
        {
            "_id": "61a8dc29fab70adfacf59789",
            "name": "CP",
            "url": "",
            "sd_subscriber_id": "",
            "account_manager": "",
            "contact_name": "",
            "contact_email": "",
            "phone": "",
            "country": "other",
            "is_enabled": true,
            "company_type": null,
            "monitoring_administrator": null,
            "allowed_ip_list": null,
            "expiry_date": null,
            "original_creator": "6183d49420d13bc4e332281d",
            "events_only": false,
            "_created": "2021-12-02T14:46:01 0000",
            "_updated": "2022-02-06T11:59:32 0000",
            "_etag": "277e2a8667b650fe4ba56f4b9b44780f3992062a",
            "archive_access": false,
            "sections": {
                "wire": true,
                "agenda": true,
                "news_api": true,
                "monitoring": true
            },
            "_links": {
                "self": {
                    "title": "Companie",
                    "href": "companies/61a8dc29fab70adfacf59789"
                },
                "related": {
                    "original_creator": {
                        "title": "User",
                        "href": "users/6183d49420d13bc4e332281d"
                    }
                }
            }
        },
        {
            "_id": "635ac6b9b837aa06e8e94ea3",
            "name": "Load Company No Exp",
            "url": "",
            "sd_subscriber_id": "",
            "account_manager": "",
            "contact_name": "",
            "contact_email": "[email protected]",
            "phone": "6478934734",
            "country": "",
            "is_enabled": true,
            "company_type": null,
            "monitoring_administrator": null,
            "allowed_ip_list": null,
            "expiry_date": null,
            "original_creator": "6298c949007f2fb1c968dfdf",
            "events_only": false,
            "_created": "2022-10-27T17:58:17 0000",
            "_updated": "2022-10-27T18:03:17 0000",
            "_etag": "9cb17d520b3ca9dc1c3326a1ccab8bbb5e7839f2",
            "version_creator": "6183d49420d13bc4e332281d",
            "_links": {
                "self": {
                    "title": "Companie",
                    "href": "companies/635ac6b9b837aa06e8e94ea3"
                },
                "related": {
                    "original_creator": {
                        "title": "User",
                        "href": "users/6298c949007f2fb1c968dfdf"
                    },
                    "version_creator": {
                        "title": "User",
                        "href": "users/6183d49420d13bc4e332281d"
                    }
                }
            }
        }
    ]

How to get it.

I tried getting it as

temp = response['_items']  

but it wont work

Please help me out.

CodePudding user response:

You need to convert raw byte string to Python dict first, assuming that you are using Python version 3.6 and your response object is either string or bytes:

import json

data = json.loads(response) # loads() decodes it to dict
temp = response['_items'] 
  • Related