0

I'm trying to convert a JSON file over to a CSV file but when I run the build it only returns 2 lines the headers and the first entry.

Not sure whats happening i've tried a few different variations but nothing seems to work, not sure if it's because the file is to big or not 6+ million lines...

import json
import csv


with open('result.json') as jsonfile:
data=jsonfile.read()

#ParseFile 
jsonobj = json.loads(data)

keylist = []
for key in jsonobj[0]:
keylist.append(key)

f = csv.writer(open("test-kyle.csv", "w"))
f.writerow(keylist)

for record in jsonobj:
currentrecord = []

for key in keylist:
    currentrecord.append(record[key])

f.writerow(currentrecord)

Here is the example JSON file:

[
    {
        "text": "<@U48TMD5QS> has joined the channel", 
        "ts": "1491388552.433852"
    }, 
    {
        "text": "*Channel: failed_signup* \nPhone number 123218736 failed to sign up on UGANDA", 
        "ts": "1491477391.593892"
    }, 
    {
        "text": "*Channel: failed_signup* \nPhone number 723880908 failed to sign up on UGANDA", 
        "ts": "1491477392.594092"
    }, 
    {
        "text": "*Channel: failed_signup* \nPhone number 723880908 failed to sign up on UGANDA", 
        "ts": "1491477393.594269"
    }, 
    {
        "text": "*Channel: failed_signup* \nPhone number 723880666 failed to sign up on UGANDA", 
        "ts": "1491477393.594395"
    }, 
    {
        "text": "*Channel: IT_ALERTS_GMAIL* \n[kve-t460] Failed to complete import cycle", 
        "ts": "1491477394.594630"
    }, 
    {
        "text": "*Channel: failed_signup* \nPhone number abcdefg failed to sign up on UGANDA", 
        "ts": "1491477434.604899"
    }, 
    {
        "text": "<@U1Y9UJD8V> has joined the channel", 
        "ts": "1493358499.130025"
    }
]
Kyle McBride
  • 171
  • 1
  • 8
  • Looks very related to https://stackoverflow.com/questions/1871524/how-can-i-convert-json-to-csv see if this question helps – krmckone Oct 22 '19 at 17:45
  • 2
    You have failed to reproduce your actual code intentation here, which in the case of Python makes your code utterly meaningless. I suspect that very last line isn't indented properly (so that it executes only once, rather than once per record), but there's no way to tell. – jasonharper Oct 22 '19 at 17:46
  • @jasonharper While that's most likely true, as it stands, he did produce a minimal, reproducible example and the only thing that's different is that we on SO get a different error from him. Either way, we can still use his code to give him the desired output, see my answer. – AlgoRythm Oct 23 '19 at 02:33

4 Answers4

0

Your indentation is messed up quite a bit. Just be careful, your code works otherwise:

import json
import csv


with open('result.json') as jsonfile:
    data=jsonfile.read()

#ParseFile 
jsonobj = json.loads(data)

keylist = []
for key in jsonobj[0]:
    keylist.append(key)

f = csv.writer(open("test-kyle.csv", "w"))
f.writerow(keylist)

for record in jsonobj:
    currentrecord = []

    for key in keylist:
        currentrecord.append(record[key])

    f.writerow(currentrecord)
AlgoRythm
  • 1,196
  • 10
  • 31
0

You need to correct your for loop

for key in jsonobj:
    keylist.append(key)
Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34
-1

Check your indentation on the two for loops on you code!

-2

Python needs 4 spaces for indentation. If your code isn't indented then your keyList is empty and it just skips the loop

for key in jsonobj[0]:
keylist.append(key)
R10t--
  • 803
  • 7
  • 16
  • 3
    Python does not have a standard number of spaces required; any consistent quantity is acceptable. – Prune Oct 22 '19 at 17:48
  • If it is a paste error into stack overflow that's understandable, but if they're actually missing the spaces it explains his problem, not sure why there's downvotes. – R10t-- Oct 22 '19 at 17:50