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.5 documentation
Convert the object to a JSON string. Note NaN’s and None will be converted to null and datetime objects will be converted to UNIX timestamps. ... String, path object (implementing os.PathLike[str]), or file-like object implementing a write() function.
GeeksforGeeks
geeksforgeeks.org › python › exporting-pandas-dataframe-to-json-file
Exporting Pandas DataFrame to JSON File - GeeksforGeeks
DSA Python · Data Science · NumPy · Pandas · Practice · Django · Flask · Last Updated : 9 Apr, 2026 · To export a Pandas DataFrame to a JSON file we use to_json() function.
Published: April 9, 2026
04:15
12. Write Dataframe to a JSON File | Using PySpark - YouTube
13:54
6. Write DataFrame into json file using PySpark | Azure Databricks ...
19:51
How to Convert & Save Pandas DataFrame to Json String, Array, File, ...
02:48
How to Use toJSON() in PySpark - Convert DataFrame Rows to JSON ...
05:03
Pandas Tutorial | How to convert a Pandas DataFrame object to a ...
04:48
Converting Complex JSON to Pandas DataFrame - YouTube
W3Schools
w3schools.com › python › pandas › pandas_json.asp
Pandas Read JSON
In our examples we will be using a JSON file called 'data.json'. Open data.json. ... Tip: use to_string() to print the entire DataFrame. ... JSON objects have the same format as Python dictionaries.
Python Guides
pythonguides.com › how-to-convert-python-dataframe-to-json
Convert A DataFrame To JSON In Python (6 Methods)
May 28, 2025 - The simplest way to convert a DataFrame to JSON is by using the built-in to_json() method in Python.
Python Examples
pythonexamples.org › pandas-convert-dataframe-to-json
Pandas – Convert DataFrame to JSON
The resulting JSON String is written to the specified file path. import pandas as pd df = pd.DataFrame({'name':['a', 'b', 'c'], 'age' :[20, 21, 20]}) df.to_json(path_or_buf='output.json')
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)
Pandas
pandas.pydata.org › docs › reference › api › pandas.read_json.html
pandas.read_json — pandas 3.0.5 documentation - PyData |
Convert a DataFrame to a JSON string.
Data to Fish
datatofish.com › export-pandas-dataframe-json
How to Export a pandas DataFrame as a JSON File
import pandas as pd data = {'fish': ['salmon', 'pufferfish', 'shark'], 'count': [100, 10, 1], } df = pd.DataFrame(data) print(df) ... import os desktop_path = os.path.expanduser('~/Desktop') df.to_json(desktop_path + '/fish.json', orient='columns') After you run this, you should find a fish.json ...
Top answer 1 of 2
3
First create Series with set_index and then Series.to_json:
j = df.set_index('a')['b'].to_json()
print (j)
{"P1":7950,"P2":1274,"P3":6160}
For file:
df.set_index('a')['b'].to_json(filename)
2 of 2
-3
There's a df.to_json() method available
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_json.html
The examples in the documentation show the different formats you may wish to output the json object in.
Pandas
pandas.pydata.org › pandas-docs › version › 2.1 › reference › api › pandas.DataFrame.to_json.html
pandas.DataFrame.to_json — pandas 2.1.4 documentation
Convert the object to a JSON string. Note NaN’s and None will be converted to null and datetime objects will be converted to UNIX timestamps. ... String, path object (implementing os.PathLike[str]), or file-like object implementing a write() function.
Python Basics
pythonbasics.org › home › pandas › json with python pandas
JSON with Python Pandas - pythonbasics.org
To do so, use the method to_json(filename). If you want to save to a json file, you can do the following: ... import pandas as pd import json data = [['Axel',32], ['Alice', 26], ['Alex', 45]] df = pd.DataFrame(data,columns=['Name','Age']) df.to_json('example.json')
w3resource
w3resource.com › pandas › dataframe › dataframe-to_json.php
Pandas DataFrame: to_json() function - w3resource
May 27, 2026 - The to_json() function is used to convert the object to a JSON string. Note: NaN's and None will be converted to null and datetime objects will be converted to UNIX timestamps. ... DataFrame.to_json(self, path_or_buf=None, orient=None, ...
Moonbooks
en.moonbooks.org › Articles › How-to-store-a-pandas-dataframe-into-a-json-file-
How to store a pandas dataframe into a json file ? - Moonbooks
import json res = df.to_json(orient="split") res_d = json.loads(res) with open('data.json', 'w') as fp: json.dump(res_d, fp) ... Earth observation scientist and developer, I explore satellite data, fire detection, and environmental analytics using Python and machine learning.
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.read_json.html
pandas.read_json — pandas 2.2.3 documentation - PyData |
Convert a DataFrame to a JSON string.
Databricks
api-docs.databricks.com › python › pyspark › latest › pyspark.pandas › api › pyspark.pandas.DataFrame.to_json.html
pyspark.pandas.DataFrame.to_json — PySpark master documentation
If ‘orient’ is ‘records’ write out line delimited json format. Will throw ValueError if incorrect ‘orient’ since others are not list like. It should be always True for now. ... It should be always ‘records’ for now. compression{‘gzip’, ‘bz2’, ‘xz’, None} A string representing the compression to use in the output file, only used when the first argument is a filename.