I have JSON data stored in the variable
obj = open('data.txt', 'wb')
TypeError: must be string or buffer, not dict
You forgot the actual JSON part -
data is a dictionary and not yet JSON-encoded. Write it like this:
import json with open('data.txt', 'w') as outfile: json.dump(data, outfile)
Note: Works on both 3.x and 2.x .