Requiesit Requiesit - 5 months ago 8
JSON Question

JSON to text file using Python

I have searched for a solution regarding this issue and I have not found one that I can understand. I am new to Python and need basic help understanding why I get the error message: TypeError: is not JSON serializable.

import requests
import json

r = requests.get("http://api.bls.gov/publicAPI/v2/timeseries/data/LAUCN040010000000005")

with open("C:\...MyPath...\Output.txt", "w") as outfile:
json.dumps(r, outfile)


This is my simple code I am testing. I appreciate the help.

Answer

You don't need to convert it to/from json. Just keep it as text.

import requests

r = requests.get("http://api.bls.gov/publicAPI/v2/timeseries/data/LAUCN040010000000005")

with open("C:\Users\mhoward2\Documents\Python Scripts\Output.txt", "w") as outfile:
    outfile.write(r.text)