0

I am trying to write out a csv file from data in JSON format. I can get the fieldnames to write to the csv file but not the item value I need. This is my first time coding in python so any help would be appreciated. The json file can be found below for reference:

https://data.ny.gov/api/views/nqur-w4p7/rows.json?accessType=DOWNLOAD

Here is my error:

Traceback (most recent call last):
  File "ChangeDataType.py", line 5, in <module>
    data = json.dumps(inputFile)
  File "/usr/lib64/python3.4/json/__init__.py", line 230, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib64/python3.4/json/encoder.py", line 192, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib64/python3.4/json/encoder.py", line 250, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib64/python3.4/json/encoder.py", line 173, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <_io.TextIOWrapper name='rows.json?accessType=DOWNLOAD' mode='r' encoding='UTF-8'> is not JSON serializable

Here is my code:

import json
import csv

inputFile = open("rows.json?accessType=DOWNLOAD", "r")
data = json.dumps(inputFile)

with open("Data.csv","w") as csvfile:

    writer = csv.DictWriter(csvfile, extrasaction='ignore', fieldnames=["date", "new_york_state_average_gal", "albany_average_gal", "binghamton_average_gal",\
 "buffalo_average_gal", "nassau_average_gal", "new_york_city_average_gal", "rochester_average_gal", "syracuse_average_gal","utica_average_gal"])

    writer.writeheader()

    for row in data:
        writer.writerow([row["date"], row["new_york_state_average_gal"], row["albany_average_gal"], row["binghamton_average_gal"],\
                         row["buffalo_average_gal"], row["nassau_average_gal"], row["new_york_city_average_gal"], row["rochester_average_gal"], row["syracuse\
_average_gal"],row["utica_average_gal"]])
8
  • dumps is for exporting json. You're trying to import it - use loads. Commented Dec 1, 2017 at 2:54
  • @Shadow That is what I originally had it as, but it was returning JSON must be string not 'TextI0Wrapper' Commented Dec 1, 2017 at 2:55
  • 2
    load without the s, the s is when you are loading from a string instead of a file. Commented Dec 1, 2017 at 2:56
  • Cause you're passing in the file pointer rather than the contents of the file. Try json.load if you're going to do it that way. Commented Dec 1, 2017 at 2:56
  • Why does your file end with ?accessType=DOWNLOAD? Commented Dec 1, 2017 at 2:58

2 Answers 2

1

If you want to read a JSON file you should use json.load instead of json.dumps:

data = json.load(inputFile)
Sign up to request clarification or add additional context in comments.

Comments

0

Seems you're still having problems even opening the file. Python json to CSV

You were told to use json.load

dumps takes an object to a string. You want to read JSON to a dictionary.

You therefore need to load the JSON file, and you can open two files at once

with open("Data.csv","w") as csvfile, open("rows.json?accessType=DOWNLOAD") as inputfile:
    data = json.load(inputfile)
    writer = csv.DictWriter(csvfile,...

Also, for example, considering the JSON data looks like "fieldName" : "syracuse_average_gal", and that is the only occurrence of the Syracuse average value, row["syracuse_average_gal"] is not correct.

Carefully inspect your JSON and figure out to parse it from the very top bracket

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.