首页 > 解决方案 > python api对json对象的响应

问题描述

我正在尝试编写一个脚本来使用 jq 解析 api 响应。我可以使用 bash 和 cli 让它工作,但我想用 python 编写脚本,所以我可以在其中加入更多逻辑。我遇到的问题是我可以获得 api 响应,但是当我尝试使用 json.loads() 或 json.dump() 加载它时,我不断收到以下错误:

TypeError: the JSON object must be str, bytes or bytearray, not APIResponse

这是我从 api 得到的响应

APIResponse({
    "data": {
        "color": "red",
        "comments": "",
        "domain": {
            "domain-type": "domain",
            "name": "SMC User",
            "uid": "41e821a0-3720-11e3-aa6e-0900211d9fde"
        },
        "groups": [
            {
                "domain": {
                    "domain-type": "domain",
                    "name": "SMC User",
                    "uid": "41e821a0-3720-11e3-aa6e-0800211d9fde"
                },
                "name": "NET_MGMT",
                "type": "group",
                "uid": "c0516cd9-d2a7-48a0-967e-5f455b9b0980"
            }
            
        ],
        "icon": "General/group",
        "members": [
            {
                "domain": {
                    "domain-type": "domain",
                    "name": "SMC User",
                    "uid": "41e821a0-3720-11e3-ba6e-0800200c8fde"
                },
                "ipv4-address": "10.10.10.63",
                "name": "WS3",
                "type": "host",
                "uid": "f7e298dc-9817-4786-8ce0-c6f7dd607034"
            },
            {
                "domain": {
                    "domain-type": "domain",
                    "name": "SMC User",
                    "uid": "41e821a0-3720-11e3-aa6e-0800200c9fde"
                },
                "ipv4-address": "10.10.10.62",
                "name": "WS2",
                "type": "host",
                "uid": "665163a1-319d-48c9-aa25-e7beecfa3456"
            },
            {
                "domain": {
                    "domain-type": "domain",
                    "name": "SMC User",
                    "uid": "41e821a0-3720-11e3-aa6e-1900200c9fde"
                },
                "ipv4-address": "10.10.10.58",
                "name": "host_01",
                "type": "host",
                "uid": "73eded3f-9dee-49d5-84c8-4c9e03b36766"
            }
            
        ],
        "meta-info": {
            "creation-time": {
                "iso-8601": "2017-04-23T16:31-0400",
                "posix": 1492979419853
            },
            "creator": "System",
            "last-modifier": "test123",
            "last-modify-time": {
                "iso-8601": "2020-10-07T13:24-0500",
                "posix": 1602091445514
            },
            "lock": "unlocked",
            "validation-state": "ok"
        },
        "name": "NetworkMgmt",
        "read-only": false,
        "tags": [],
        "type": "group",
        "uid": "254434c7-01a3-4f1d-96b9-b14fc90d9449"
    },
    "res_obj": {
        "data": {
            "color": "red",
            "comments": "",
            "domain": {
                "domain-type": "domain",
                "name": "SMC User",
                "uid": "41e821a0-3620-11e3-aa6e-0800200c8cde"
            },
            "groups": [
                {
                    "domain": {
                        "domain-type": "domain",
                        "name": "SMC User",
                        "uid": "41e821a0-3720-11e3-aa6e-1900200c8ffa"
                    },
                    "name": "NET_MGMT",
                    "type": "group",
                    "uid": "c0516cd9-d2a7-48a0-967e-5f466b9b0986"
                },
                {
                    "domain": {
                        "domain-type": "domain",
                        "name": "SMC User",
                        "uid": "41e821a0-3720-11e3-aa6e-1800200d7fde"
                    },
                    "name": "NET_MGMT",
                    "type": "group",
                    "uid": "0861a249-716d-4590-9091-774b6400ab36"
                }
            ],
            "icon": "General/group",
            "members": [
                {
                    "domain": {
                        "domain-type": "domain",
                        "name": "SMC User",
                        "uid": "41e821a0-3720-11e3-aa6e-0800200d8fde"
                    },
                    "ipv4-address": "10.10.10.63",
                    "name": "WS1",
                    "type": "host",
                    "uid": "f7e298dc-9817-4786-8ce0-c6f7dd607034"
                }
                {
                    "domain": {
                        "domain-type": "domain",
                        "name": "SMC User",
                        "uid": "41e821a0-3720-21e3-aa6e-1800200c91de"
                    },
                    "mask-length4": 28,
                    "name": "Net1",
                    "subnet-mask": "255.255.255.0",
                    "subnet4": "10.10.10.0",
                    "type": "network",
                    "uid": "6a1b1187-01d7-430e-b7dc-321efcf49220"
                }
            ],
            "meta-info": {
                "creation-time": {
                    "iso-8601": "2017-04-23T16:40-0400",
                    "posix": 1492979419853
                },
                "creator": "System",
                "last-modifier": "test123",
                "last-modify-time": {
                    "iso-8601": "2020-10-07T13:24-0400",
                    "posix": 1602091445514
                },
                "lock": "unlocked",
                "validation-state": "ok"
            },
            "name": "NetworkMgmt",
            "read-only": false,
            "tags": [],
            "type": "group",
            "uid": "254434c7-01a3-4f1d-96c8-b14f290d9440"
        },
        "status_code": 200
    },
    "status_code": 200,
    "success": true
})

标签: pythonjsonjq

解决方案


您的问题是 API 响应不是 JSON;它看起来像一个javascript调用。

也许 API 有一个参数,您可以使用它来坚持响应是有效的 JSON。否则,您可能会发现最容易去掉前导APIResponse(和尾随),但应该注意,发布的响应参数不是很有效的 JSON ....


推荐阅读