json.dumps() not working

json, python-2.7

Solution

This isn't how `json.dumps()` works. `json.dumps()` returns a string, which you must then write into the file using `f.write()`. Like so:

with open(path, 'w') as f:
    json_str = json.dumps({'profile_info': prof_info})
    f.write(json_str)

Or, just use `json.dump()`, which exists exactly for the purpose of dumping JSON data into a file descriptor.

with open(path, 'w') as f:
    json.dump({'profile_info': prof_info}, f)

Problem

``` import json def json_serialize(name, ftype, path): prof_info = [] prof_info.append({ 'profile_name': name, 'filter_type': ftype }) with open(path, "w") as f: json.dumps({'profile_info': prof_info}, f) json_serialize(profile_name, filter_type, "/home/file.json") ``` The above code doesn't dumps the data into the "file.json" file. When I write `print` before `json.dumps()`, then the data gets printed on the screen. But it doesn't get dumped into the file. The file gets created but on opening it (using notepad), there is nothing. Why? How to correct it?

Original source