python change 0 value [closed]

I am getting data with rest api. I want to replace all 0 values with null(or another number) in response outputs “data” section.

headers = {
    "cookie": "JSESSIONID=....",
    "Authorization": "Basic ...",
    "Content-Type": "application/json"
}

response = requests.request("POST", url, json=payload, headers=headers, params=querystring, verify=False).text

Outputof response:

{
    "status": {
        "total": 1,
        "matched": 1,
        "processed": 1,
        "completed": true,
        "aborted": false,
        "hasErrors": false,
    },
    "result": [
        {
            "signature": "raidOwner#33414-SACONC1ANKT",
            "syn_raidLdev_transferRate": [
                {
                    "type": "timeseries",
                    "name": "Transfer Rate",
                    "unit": "KB/s",
                    "interval": 300,
                    "start": "20210627_230500",
                    "data": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                }
            ],
        },
        {
            "signature": "raidOwner#33414-BUTTE08",
            "syn_raidLdev_transferRate": [
                {
                    "type": "timeseries",
                    "name": "Transfer Rate",
                    "unit": "KB/s",
                    "interval": 300,
                    "start": "20210627_230500",
                    "data": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                }
            ],
        },
        {
            "signature": "raidOwner#33414-SREDMMAPP5",
            "syn_raidLdev_transferRate": [
                {
                    "type": "timeseries",
                    "name": "Transfer Rate",
                    "unit": "KB/s",
                    "interval": 300,
                    "start": "20210627_230500",
                    "data": [0, 3, 0, 3, 0, 3, 3, 0, 3, 0, 3, 0],
                }
            ],
        },
        {
            "signature": "raidOwner#33414-BUTTE01",
            "syn_raidLdev_transferRate": [
                {
                    "type": "timeseries",
                    "name": "Transfer Rate",
                    "unit": "KB/s",
                    "interval": 300,
                    "start": "20210627_230500",
                    "data": [
                        1814,
                        1609,
                        1279,
                        1394,
                        1320,
                        1249,
                        2002,
                        1329,
                        1270,
                        1359,
                        1284,
                        3383,
                    ],
                }
            ],
        },
    ],
}

I need to replace all 0 values with null in “data”: [] values. How can I do that?

Answer

This solution will work only on data that has required structure.

data = {} # omitted

for v in data["result"]:
    for v in v["syn_raidLdev_transferRate"]:
        dt = v["data"]
        for i, v in enumerate(dt):
            if v == 0:
                dt[i] = None

print(data)

additionally, there is how you save resulting data

import json 

     
with open("result.json", "w") as outfile: 
    json.dump(data, outfile)