Juan Almada
Juan Almada

Reputation: 189

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

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}
    ]
}

Upvotes: 0

Views: 58

Answers (1)

Tom McLean
Tom McLean

Reputation: 6347

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"])

Upvotes: 2

Related Questions