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')
Answer from Brad Solomon on Stack OverflowPandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.to_json.html
pandas.DataFrame.to_json — pandas 3.0.1 documentation
Handler to call if object cannot otherwise be converted to a suitable format for JSON. Should receive a single argument which is the object to convert and return a serialisable object. ... If ‘orient’ is ‘records’ write out line-delimited json format.
Top answer 1 of 9
115
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')
2 of 9
101
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)
Skytowner
skytowner.com › explore › pandas_dataframe_to_json_method
Pandas DataFrame | to_json method with Examples
The path to where you want to save the JSON. By default, the method will return a JSON string without writing to a file. ... How you want convert the source DataFrame into a JSON. ... Here, we call each item a record. ... By default, orient="columns".
GitHub
github.com › pandas-dev › pandas › issues › 29928
Using to_json/read_json with orient='table' on a DataFrame with a single level MultiIndex does not work · Issue #29928 · pandas-dev/pandas
November 29, 2019 - js=df.to_json() pandas.read_json(js) #ok js=df.to_json(orient='table') new_df=pandas.read_json(js, orient='table') # This is a workaround that does produce sensible results
Author larrymouse
w3resource
w3resource.com › pandas › dataframe › dataframe-to_json.php
Pandas DataFrame: to_json() function - w3resource
August 19, 2022 - DataFrame.to_json(self, path_or_buf=None, orient=None, date_format=None, double_precision=10, force_ascii=True, date_unit='ms', default_handler=None, lines=False, compression='infer', index=True) Parameters: Returns: None or str If path_or_buf is None, returns the resulting json format as a string.
Apache
spark.apache.org › docs › latest › api › python › reference › pyspark.pandas › api › pyspark.pandas.DataFrame.to_json.html
pyspark.pandas.DataFrame.to_json — PySpark 4.1.1 documentation
output JSON format is different from pandas’. It always uses orient=’records’ for its output. This behavior might have to change soon.
Spark Code Hub
sparkcodehub.com › pandas-dataframe-to-json-guide
Converting Pandas DataFrame to JSON: A Comprehensive Guide
data = { 'Name': ['Alice', 'Bob'], 'Details': [{'id': 1}, {'id': 2}], 'Hire_Date': [pd.to_datetime('2023-01-15'), pd.to_datetime('2022-06-20')] } df = pd.DataFrame(data) json_str = df.to_json(orient='records', date_format='iso') print(json_str)
Pandas
pandas.pydata.org › pandas-docs › version › 0.24.2 › reference › api › pandas.DataFrame.to_json.html
pandas.DataFrame.to_json — pandas 0.24.2 documentation
>>> df.to_json(orient='records') '[{"col 1":"a","col 2":"b"},{"col 1":"c","col 2":"d"}]'
GitHub
github.com › pandas-dev › pandas › issues › 50456
BUG: JSON serialization with orient split fails roundtrip with MultiIndex · Issue #50456 · pandas-dev/pandas
December 28, 2022 - >>> df = DataFrame([[1, 2], [3, 4]], ... columns=pd.MultiIndex.from_arrays([["2022", "2022"], ['JAN', 'FEB']])) >>> df 2022 JAN FEB 0 1 2 1 3 4 >>> read_json(df.to_json(orient='split'), orient='split') 2022 JAN 2022 FEB 0 1 2 1 3 4
Author datapythonista
GitHub
github.com › pandas-dev › pandas › issues › 17566
DataFrame to_json Column -> [values] orientation and JSON able · Issue #17566 · pandas-dev/pandas
September 18, 2017 - DataFrame to_json Column -> [values] orientation and JSON able#17566 · Copy link · Labels · IO JSONread_json, to_json, json_normalizeread_json, to_json, json_normalizeUsage Question · Wall-ee · opened · on Sep 18, 2017 · Issue body actions · #7863 has mentioned that to_dict(orient='list') could be a solution.
Author Wall-ee
pandas
pandas.pydata.org › pandas-docs › dev › reference › api › pandas.DataFrame.to_json.html
pandas.DataFrame.to_json — pandas 3.0.0rc1+117.gff0cd9a3a7 documentation
Handler to call if object cannot otherwise be converted to a suitable format for JSON. Should receive a single argument which is the object to convert and return a serialisable object. ... If ‘orient’ is ‘records’ write out line-delimited json format.