0

I have data in Pandas dataframe and I am able to write the dataframe data into JSON file by calling:

df.to_json('filepath', orient='records')

This writes data into json file as an array of JSON objects.

[{"col 1":"a","col 2":"b"},{"col 1":"c","col 2":"d"}]

I want data in the json file to be as below i.e. just comma seperated JSON objects without array

{"col 1":"a","col 2":"b"},{"col 1":"c","col 2":"d"}

Would appreciate any help. I am new to python and not able to find the way. Thank you.

cs95
  • 379,657
  • 97
  • 704
  • 746
A Baldino
  • 178
  • 1
  • 11
  • what you want is not valid JSON ... and Pandas knows that – RomanPerekhrest Oct 26 '17 at 15:59
  • @RomanPerekhrest http://jsonlines.org/ technically, it isn't _invalid_... – cs95 Oct 26 '17 at 16:06
  • @cᴏʟᴅsᴘᴇᴇᴅ, try ANY JSON processor or JSON validator - and you'll understand. What he wants is text, not valid JSON – RomanPerekhrest Oct 26 '17 at 16:09
  • @RomanPerekhrest True that it isn't valid *JSON*, but it's a valid _JSON file format_, that's all I meant to say, sorry for the confusion. – cs95 Oct 26 '17 at 16:09
  • @RomanPerekhrest Also, I assumed this is how OP wants it written to the file, seeing as they are calling `to_json` with a filepath. – cs95 Oct 26 '17 at 16:10
  • @cᴏʟᴅsᴘᴇᴇᴅ Thanks for mentioning that, the output file is processed by another program and it is failing with an array of JSON objects. :( – A Baldino Oct 26 '17 at 19:03

1 Answers1

1

Ah, so you want a JSON line file. You could do that in a similar fashion. Call to_dict and write to a file in a loop.

with open('file.json', 'w') as f:
    for x in df.to_dict(orient='r'):
        f.write(json.dumps(x) + '\n')

Alternatively, call to_json in a loop:

with open('file.json', 'w') as f:
    for _, r in df.iterrows():
        r.to_json(f); f.write('\n')
cs95
  • 379,657
  • 97
  • 704
  • 746