# AttributeError: 'str' object has no attribute 'keys'

While converting json file to csv,I got the above error My code is shown below:

import json
import csv
data = open("SRInFlow.json",encoding="utf8")
r=rows.values()
fieldnames = [b'dateTimeValues', b'timeSeries']
with open('C:/DeepLearning/tensorflowpoc/Codes/SRInFlow.csv', 'w') as f:
dict_writer = csv.DictWriter(f, fieldnames=fieldnames)
for d in r:
--->    dict_writer.writerow(d)


The error displays in the last line. How to rectify the error? Thanks in advance

• It depends on your JSON structure – Aditya Mar 9 '18 at 12:24
• Try printing r and type(r) to get an idea what you are dealing with. – Elias Strehle Mar 9 '18 at 12:31
• type(r) is <class 'dict_values'> – Sheetal Mar 13 '18 at 5:51
• r = rows.values() ... for d in r: is a really bad code-smell that you're trying to iterate over just the row values, yet expect them to behave like a dict(!) Why not just simply iterate the normal way: for row in rows: – smci May 24 '19 at 23:15

This error is because you are initialising a dictionary writer dict_writer = csv.DictWriter(f, fieldnames=fieldnames) but then you are passing a normal row containing only the values r=rows.values() -> for d in r: ->dict_writer.writerow(d), when the writer expects a dictionary including the keys. That is what the error means.

I don't know how your JSON looks but you can try something like this:

import json
import csv

data = '{"dateTimeValues": [1,3,4], "timeSeries": [0,0,0]}'

r=zip(*rows.values())

fieldnames = ['dateTimeValues', 'timeSeries']
with open('c:/Users/jsz.P3-GROUP/Desktop/shit.csv', 'w') as f:
dict_writer = csv.writer(f)
dict_writer.writerow(fieldnames)
for d in r:
dict_writer.writerow(d)


This initialises a simple csv writer dict_writer = csv.writer(f)(not dictionary), then inputs the fieldnames as a normal row for the header dict_writer.writerow(fieldnames) and finally inserts only the values as in your example.

Note that for my json string I had to transpose the values first as in r=zip(*rows.values()).

Hope this helps.

• Thanks... but got an another error ''_csv.Error: iterable expected, not int ''. My json data is very large which contains 5-6 years of data and has two headings dateTimeValues and timeSeries – Sheetal Mar 13 '18 at 5:49
• Have a look at this: stackoverflow.com/questions/33550125/… – TitoOrt Mar 13 '18 at 9:18