Home > Software design >  how to parse a json that contains a list to a more readable json with python?
how to parse a json that contains a list to a more readable json with python?

Time:10-29

this time I have some strange json that I want to convert to something more readable but I don't know how to do it in Python language:

Current json format:

{'data': [{'VALUE': '{"filters":[ {"field":"example1","operation":"like","values":["Completed"]},{"field":"example2","operation":"like","values":["value1","value2","value3"]}]}'}]}

Json that I want to obtain for further data processing:

{
    "filters": [
        {
            "field": "example1",
            "operation": "like",
            "values": [
                "Completed"
            ]
        },
        {
            "field": "example2",
            "operation": "like",
            "values": [
                "value1",
                "value2",
                "value3",
            ]
        }
    ]
}

CodePudding user response:

Try:

import json

data = {
    "data": [
        {
            "VALUE": '{"filters":[ {"field":"example1","operation":"like","values":["Completed"]},{"field":"example2","operation":"like","values":["value1","value2","value3"]}]}'
        }
    ]
}

dct = json.loads(data["data"][0]["VALUE"])
print(dct)

Prints:

{
    "filters": [
        {"field": "example1", "operation": "like", "values": ["Completed"]},
        {
            "field": "example2",
            "operation": "like",
            "values": ["value1", "value2", "value3"],
        },
    ]
}
  • Related