Search code examples
pythonequivalent

what is the equivalent function of res.json() in python


"I have to implement a web hook for api.ai which takes json as output from the web hook". How to give asynchronous output in python equivalent in node [res.json(...)]. I need to know res.json() equivalent in python.


Solution

  • It is just json which ships with Python. It serializes data to a string. That string can then be read back in. Most languages support json.

    import json
    
    data = [1, 2]
    str_data = json.dumps(data)  # "[1, 2]"
    value = json.loads(str_data)
    print(value == data)