1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] How to parse json string values that contain double quotes? [closed]

Discussão em 'Python' iniciado por Stack, Setembro 12, 2024.

  1. Stack

    Stack Membro Participativo

    json_str = '{"name": "This is a "test"", "description": "Another "example""}'
    json_dict = json.loads(json_str)


    For example, "test" and "example" are double quoted which break json format. How to parse this correctly? My goal is to produce a valid json str which can be loaded as a dict object.

    If I first use:

    json_str = json.dumps(json_str)


    The output is:

    '"{\\"name\\": \\"This is a \\"test\\"\\", \\"description\\": \\"Another \\"example\\"\\"}"'


    However, now if I load it,

    json_str = json.loads(json_str)


    It becomes the original str, but not a dict object:

    '{"name": "This is a "test"", "description": "Another "example""}'


    I think this should be a very common issue when dealing with json data, but I didn't find a solution to do it. Isn't a standard way to do this?

    Continue reading...

Compartilhe esta Página