Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中将部分api响应转换为完整的json_Python_Json_Python 3.x_String_Api - Fatal编程技术网

如何在python中将部分api响应转换为完整的json

如何在python中将部分api响应转换为完整的json,python,json,python-3.x,string,api,Python,Json,Python 3.x,String,Api,我已经成功地从系统中获取了数据,但是其中一个字段作为管道分隔数据的长字符串返回,而不是json格式 这是python脚本中的api调用和响应处理: info = requests.request("GET", url, headers=headers, verify=False) data = info.json() return(data) 这是响应数据的净化版本。我关心的字段是“属性”字段中的数据。如何将其转换为嵌套js

我已经成功地从系统中获取了数据,但是其中一个字段作为管道分隔数据的长字符串返回,而不是json格式

这是python脚本中的api调用和响应处理:

        info = requests.request("GET", url, headers=headers, verify=False)
        data = info.json()

        return(data)
这是响应数据的净化版本。我关心的字段是“属性”字段中的数据。如何将其转换为嵌套json数组

[
    {
        "id": 00000001,
        "name": "hostname1",
        "type": "HostRecord",
        "properties": "absoluteName=hostname1.mydomain.com|parentId=00000010|parentType=Zone|reverseRecord=false|addresses=10.10.10.11|addressIds=00000100|"
    },
    {
        "id": 00000002,
        "name": "hostname2",
        "type": "HostRecord",
        "properties": "absoluteName=hostname2.mydomain.com|parentId=00000020|parentType=Zone|reverseRecord=true|addresses=10.10.10.12|addressIds=00000200|"
    }

如果我理解正确,您希望
“absoluteName=hostname1.mydomain.com | parentId=00000010”
变成数组
[“absoluteName=hostname1.mydomain.com”,“parentId=00000010”
。如果是这样,您的解决方案非常简单:

for dct in data:
    dct['properties'] = dct['properties'].split('|')
这只是在
“|”
处拆分字符串,返回结果列表并再次将其设置为等于
属性
字段


但是,我不确定这正是你想要的。这就是您所说的“嵌套json数组”吗?

类似于下面的内容

data = [
    {
        "id": "00000001",
        "name": "hostname1",
        "type": "HostRecord",
        "properties": "absoluteName=hostname1.mydomain.com|parentId=00000010|parentType=Zone|reverseRecord=false|addresses=10.10.10.11|addressIds=00000100|"
    },
    {
        "id": "00000002",
        "name": "hostname2",
        "type": "HostRecord",
        "properties": "absoluteName=hostname2.mydomain.com|parentId=00000020|parentType=Zone|reverseRecord=true|addresses=10.10.10.12|addressIds=00000200|"
    }
]


def make_dict(properties: str) -> dict:
    result = {}
    pairs = properties.split('|')
    for pair in pairs:
        if pair:
            k, v = pair.split('=')
            result[k] = v
    return result


for entry in data:
    entry['properties'] = make_dict(entry['properties'])
print(data)
输出

[{'id': '00000001', 'name': 'hostname1', 'type': 'HostRecord', 'properties': {'absoluteName': 'hostname1.mydomain.com', 'parentId': '00000010', 'parentType': 'Zone', 'reverseRecord': 'false', 'addresses': '10.10.10.11', 'addressIds': '00000100'}}, {'id': '00000002', 'name': 'hostname2', 'type': 'HostRecord', 'properties': {'absoluteName': 'hostname2.mydomain.com', 'parentId': '00000020', 'parentType': 'Zone', 'reverseRecord': 'true', 'addresses': '10.10.10.12', 'addressIds': '00000200'}}]
试试这个:

a = [
{
    "id": 1,
    "name": "hostname1",
    "type": "HostRecord",
    "properties":"absoluteName=hostname1.mydomain.com|parentId=00000010|parentType=Zone|reverseRecord=false|addresses=10.10.10.11|addressIds=00000100|",
},
{
    "id": 2,
    "name": "hostname2",
    "type": "HostRecord",
    "properties": "absoluteName=hostname2.mydomain.com|parentId=00000020|parentType=Zone|reverseRecord=true|addresses=10.10.10.12|addressIds=00000200|",
},
]

for list_item in a:
    temp_dict = {}
    for item in list_item["properties"].split("|"):
        if item != "":
            key1, value1 = item.split("=")
            temp_dict[key1] = value1
    list_item["properties"] = temp_dict

print(a)
输出:

[{'id': 1, 'name': 'hostname1', 'type': 'HostRecord', 'properties': {'absoluteName': 'hostname1.mydomain.com', 'parentId': '00000010', 'parentType': 'Zone', 'reverseRecord': 'false', 'addresses': '10.10.10.11', 'addressIds': '00000100'}}, {'id': 2, 'name': 'hostname2', 'type': 'HostRecord', 
'properties': {'absoluteName': 'hostname2.mydomain.com', 'parentId': '00000020', 'parentType': 'Zone', 'reverseRecord': 'true', 'addresses': '10.10.10.12', 'addressIds': '00000200'}}]