How to transform part of an api response to full json in python

Viewed 41

I have managed to get my data back out of a system, but one of the fields is coming back as a long string of pipe-delimited data, instead of json format.

This is the api call and response handling from my python script:

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

        return(data)

This is a sanitized version of the response data. The field that I'm concerned with is the data within the "properties" field. How can I transform this to a nested json array?

[
    {
        "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|"
    }
3 Answers

something like the below

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)

output

[{'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'}}]

Try this out:

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)

Output:

[{'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'}}]

If I'm understanding correctly, you want "absoluteName=hostname1.mydomain.com|parentId=00000010" to turn into the array ["absoluteName=hostname1.mydomain.com", "parentId=00000010"]. If that is the case, your solution is pretty simple:

for dct in data:
    dct['properties'] = dct['properties'].split('|')

This just splits the string at '|', returning the resulting list and setting it equal to the properties field again.

But, I'm not sure that's exactly what you're looking for. Is that what you meant by "nested json array"?

Related