How to change the type and value of particular field in json with python?

Viewed 21

I have this json format and I want to change the field "count" from decimal to int:

I have:

{
    "label_1": "description_harcode_here_1",
    "description_harcode_here_2": [
        {"field": "AVG_SCORE", "avg": -0.487, "count": 388545.0},
        {"field": "ASD_SCORE", "avg": -0.56, "count": 388545.0},
        {"field": "QWE_AVG", "avg": -0.171, "count": 388545.0},
        {"field": "OPO_SCORE", "avg": -0.581, "count": 388545.0},
        {"field": "HAHA_SCORE", "avg": 0.063, "count": 388545.0},
    ],
}

I want:

{
    "label_1": "description_harcode_here_1",
    "description_harcode_here_2": [
        {"field": "AVG_SCORE", "avg": -0.487, "count": 388545},
        {"field": "ASD_SCORE", "avg": -0.56, "count": 388545},
        {"field": "QWE_AVG", "avg": -0.171, "count": 388545},
        {"field": "OPO_SCORE", "avg": -0.581, "count": 388545},
        {"field": "HAHA_SCORE", "avg": 0.063, "count": 388545}
    ]
}

1 Answers

You can modify the dictionary in place:

d = {
    "label_1": "description_harcode_here_1",
    "description_harcode_here_2": [
        {"field": "AVG_SCORE", "avg": -0.487, "count": 388545.0},
        {"field": "ASD_SCORE", "avg": -0.56, "count": 388545.0},
        {"field": "QWE_AVG", "avg": -0.171, "count": 388545.0},
        {"field": "OPO_SCORE", "avg": -0.581, "count": 388545.0},
        {"field": "HAHA_SCORE", "avg": 0.063, "count": 388545.0},
    ],
}

for description in d["description_harcode_here_2"]:
    description["count"] = int(description["count"])
Related