Convert Pandas DataFrame to JSON format

JsonPandasDataframe

Json Problem Overview


I have a Pandas DataFrame with two columns – one with the filename and one with the hour in which it was generated:

 File       Hour
  F1		 1
  F1 		 2
  F2 		 1
  F3 		 1

I am trying to convert it to a JSON file with the following format:

{"File":"F1","Hour":"1"} 
{"File":"F1","Hour":"2"}
{"File":"F2","Hour":"1"}
{"File":"F3","Hour":"1"}

When I use the command DataFrame.to_json(orient = "records"), I get the records in the below format:

[{"File":"F1","Hour":"1"}, {"File":"F1","Hour":"2"}, {"File":"F2","Hour":"1"}, {"File":"F3","Hour":"1"}]

I'm just wondering whether there is an option to get the JSON file in the desired format. Any help would be appreciated.

Json Solutions


Solution 1 - Json

The output that you get after DF.to_json is a string. So, you can simply slice it according to your requirement and remove the commas from it too.

out = df.to_json(orient='records')[1:-1].replace('},{', '} {')

To write the output to a text file, you could do:

with open('file_name.txt', 'w') as f:
    f.write(out)

Solution 2 - Json

In newer versions of pandas (0.20.0+, I believe), this can be done directly:

df.to_json('temp.json', orient='records', lines=True)

Direct compression is also possible:

df.to_json('temp.json.gz', orient='records', lines=True, compression='gzip')

Solution 3 - Json

I think what the OP is looking for is:

with open('temp.json', 'w') as f:
    f.write(df.to_json(orient='records', lines=True))

This should do the trick.

Solution 4 - Json

use this formula to convert a pandas DataFrame to a list of dictionaries :

import json
json_list = json.loads(json.dumps(list(DataFrame.T.to_dict().values())))

Solution 5 - Json

Try this one:

json.dumps(json.loads(df.to_json(orient="records")))

Solution 6 - Json

convert data-frame to list of dictionary

list_dict = []

for index, row in list(df.iterrows()):
    list_dict.append(dict(row))

save file

with open("output.json", mode) as f:
    f.write("\n".join(str(item) for item in list_dict))

Solution 7 - Json

To transform a dataFrame in a real json (not a string) I use:

    from io import StringIO
    import json
    import DataFrame
    
    buff=StringIO()
    #df is your DataFrame
    df.to_json(path_or_buf=buff,orient='records')
    dfJson=json.loads(buff)
    

Solution 8 - Json

instead of using dataframe.to_json(orient = “records”) use dataframe.to_json(orient = “index”) my above code convert the dataframe into json format of dict like {index -> {column -> value}}

Solution 9 - Json

Here is small utility class that converts JSON to DataFrame and back: Hope you find this helpful.

# -*- coding: utf-8 -*-
from pandas.io.json import json_normalize

class DFConverter:
    
    #Converts the input JSON to a DataFrame
    def convertToDF(self,dfJSON):
        return(json_normalize(dfJSON))

    #Converts the input DataFrame to JSON 
    def convertToJSON(self, df):
        resultJSON = df.to_json(orient='records')
        return(resultJSON)
        

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3447653View Question on Stackoverflow
Solution 1 - JsonNickil MaveliView Answer on Stackoverflow
Solution 2 - JsonBrad SolomonView Answer on Stackoverflow
Solution 3 - JsonsagarsarView Answer on Stackoverflow
Solution 4 - JsonAmir.SView Answer on Stackoverflow
Solution 5 - Jsonchandra sutrisnoView Answer on Stackoverflow
Solution 6 - JsonHafiz Shehbaz AliView Answer on Stackoverflow
Solution 7 - JsonMiguel GomezView Answer on Stackoverflow
Solution 8 - JsonJ Rishabh KumarView Answer on Stackoverflow
Solution 9 - JsonSivaView Answer on Stackoverflow