In versions of Pandas > 0.19.0, DataFrame.to_json has a parameter, lines, that will write out JSONL format.

Given that, a more succinct version of your solution might look like this:

import pandas as pd

data = [{'label': 'DRUG', 'pattern': 'aspirin'},
        {'label': 'DRUG', 'pattern': 'trazodone'},
        {'label': 'DRUG', 'pattern': 'citalopram'}]
df = pd.DataFrame(data)

# Wrap pattern column in a dictionary
df["pattern"] = df.pattern.apply(lambda x: {"lower": x})

# Output in JSONL format
print(df.to_json(orient='records', lines=True))

Output:

{"label":"DRUG","pattern":{"lower":"aspirin"}}
{"label":"DRUG","pattern":{"lower":"trazodone"}}
{"label":"DRUG","pattern":{"lower":"citalopram"}}
Answer from kmsquire on Stack Overflow
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.to_json.html
pandas.DataFrame.to_json — pandas 3.0.1 documentation
orient='table' contains a ‘pandas_version’ field under ‘schema’. This stores the version of pandas used in the latest revision of the schema. ... >>> from json import loads, dumps >>> df = pd.DataFrame( ... [["a", "b"], ["c", "d"]], ... index=["row 1", "row 2"], ... columns=["col 1", "col 2"], ... ) >>> result = df.to_json(orient="split") >>> parsed = loads(result) >>> dumps(parsed, indent=4) {{ "columns": [ "col 1", "col 2" ], "index": [ "row 1", "row 2" ], "data": [ [ "a", "b" ], [ "c", "d" ] ] }}
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.read_json.html
pandas.read_json — pandas 3.0.1 documentation - PyData |
The encoding to use to decode py3 bytes. encoding_errorsstr, optional, default “strict” · How encoding errors are treated. List of possible values . ... Read the file as a json object per line.
🌐
OpenAI Developer Community
community.openai.com › documentation
Convert Pandas dataframe to jsonl - Documentation - OpenAI Developer Community
June 21, 2021 - Hi guys, I have a table that looks like this Feedback content 1 content 2 content 3 … How do I convert this to the jsonl in this format using pandas: {text: content 1} {text:content 2} as required by the specia…
🌐
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.Series.to_json.html
pandas.Series.to_json — pandas 2.3.3 documentation
orient='table' contains a ‘pandas_version’ field under ‘schema’. This stores the version of pandas used in the latest revision of the schema. ... >>> from json import loads, dumps >>> df = pd.DataFrame( ... [["a", "b"], ["c", "d"]], ... index=["row 1", "row 2"], ... columns=["col 1", "col 2"], ... ) >>> result = df.to_json(orient="split") >>> parsed = loads(result) >>> dumps(parsed, indent=4) { "columns": [ "col 1", "col 2" ], "index": [ "row 1", "row 2" ], "data": [ [ "a", "b" ], [ "c", "d" ] ] }
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 2.0 › reference › api › pandas.DataFrame.to_json.html
pandas.DataFrame.to_json — pandas 2.0.3 documentation
orient='table' contains a ‘pandas_version’ field under ‘schema’. This stores the version of pandas used in the latest revision of the schema. ... >>> from json import loads, dumps >>> df = pd.DataFrame( ... [["a", "b"], ["c", "d"]], ... index=["row 1", "row 2"], ... columns=["col 1", "col 2"], ... ) >>> result = df.to_json(orient="split") >>> parsed = loads(result) >>> dumps(parsed, indent=4) { "columns": [ "col 1", "col 2" ], "index": [ "row 1", "row 2" ], "data": [ [ "a", "b" ], [ "c", "d" ] ] }
🌐
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')
Find elsewhere
🌐
W3Schools
w3schools.com › python › pandas › pandas_json.asp
Pandas Read JSON
import pandas as pd data = { "Duration":{ "0":60, "1":60, "2":60, "3":45, "4":45, "5":60 }, "Pulse":{ "0":110, "1":117, "2":103, "3":109, "4":117, "5":102 }, "Maxpulse":{ "0":130, "1":145, "2":135, "3":175, "4":148, "5":127 }, "Calories":{ "0":409, "1":479, "2":340, "3":282, "4":406, "5":300 } } df = pd.DataFrame(data) print(df) Try it Yourself » ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
🌐
Skytowner
skytowner.com › explore › pandas_dataframe_to_json_method
Pandas DataFrame | to_json method with Examples
For situations like this when the conversion is improper, we can use the default_handler parameter to control what is returned: ... The handler takes as argument the source DataFrame and returns a serialisable object like a map, Series, DataFrame and so on. Now, instead of the malformed JSON that we had before, we can return another JSON of our liking. ... Here, the second line has 3 whitespaces, while the third has 6 whitespaces, and so on. ... Ask a question or leave a feedback... Official Pandas Documentation https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_json.html
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas – convert dataframe to json string
Pandas - Convert DataFrame to JSON String - Spark By {Examples}
November 5, 2024 - You can convert Pandas DataFrame to JSON string by using the DataFrame.to_json() method. This method takes a very important param orient which accepts values ‘columns‘, ‘records‘, ‘index‘, ‘split‘, ‘table‘, and ‘values‘. ...
🌐
GitHub
github.com › pandas-dev › pandas › issues › 35849
ENH: Enable `append mode` to JSON lines · Issue #35849 · pandas-dev/pandas
August 22, 2020 - Include the argument mode: str = "w"on to_json this will NOT break anything as the default behavior continues as write mode · Include a conditional in case mode="a", checking if orient='records' and lines=True, raising an Exception otherwise
Author   nullhack
Top answer
1 of 4
120

Note: Line separated json is now supported in read_json (since 0.19.0):

In [31]: pd.read_json('{"a":1,"b":2}\n{"a":3,"b":4}', lines=True)
Out[31]:
   a  b
0  1  2
1  3  4

or with a file/filepath rather than a json string:

pd.read_json(json_file, lines=True)

It's going to depend on the size of you DataFrames which is faster, but another option is to use str.join to smash your multi line "JSON" (Note: it's not valid json), into valid json and use read_json:

In [11]: '[%s]' % ','.join(test.splitlines())
Out[11]: '[{"a":1,"b":2},{"a":3,"b":4}]'

For this tiny example this is slower, if around 100 it's the similar, signicant gains if it's larger...

In [21]: %timeit pd.read_json('[%s]' % ','.join(test.splitlines()))
1000 loops, best of 3: 977 µs per loop

In [22]: %timeit l=[ json.loads(l) for l in test.splitlines()]; df = pd.DataFrame(l)
1000 loops, best of 3: 282 µs per loop

In [23]: test_100 = '\n'.join([test] * 100)

In [24]: %timeit pd.read_json('[%s]' % ','.join(test_100.splitlines()))
1000 loops, best of 3: 1.25 ms per loop

In [25]: %timeit l = [json.loads(l) for l in test_100.splitlines()]; df = pd.DataFrame(l)
1000 loops, best of 3: 1.25 ms per loop

In [26]: test_1000 = '\n'.join([test] * 1000)

In [27]: %timeit l = [json.loads(l) for l in test_1000.splitlines()]; df = pd.DataFrame(l)
100 loops, best of 3: 9.78 ms per loop

In [28]: %timeit pd.read_json('[%s]' % ','.join(test_1000.splitlines()))
100 loops, best of 3: 3.36 ms per loop

Note: of that time the join is surprisingly fast.

2 of 4
28

If you are trying to save memory, then reading the file a line at a time will be much more memory efficient:

with open('test.json') as f:
    data = pd.DataFrame(json.loads(line) for line in f)

Also, if you import simplejson as json, the compiled C extensions included with simplejson are much faster than the pure-Python json module.

🌐
Medium
sundararamanp.medium.com › a-relatively-faster-approach-for-reading-json-lines-file-into-pandas-dataframe-90b57353fd38
A relatively faster approach for reading json lines file into pandas dataframe | by Sundararaman Parameswaran | Medium
April 22, 2020 - A relatively faster approach for reading json lines file into pandas dataframe JSON-lines is a widely used file format other than CSV. The difference between JSON-lines and JSON file is only that the …
🌐
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 = pd.DataFrame([['a', 'b'], ['c', 'd']], ... index=['row 1', 'row 2'], ... columns=['col 1', 'col 2']) >>> df.to_json(orient='split') '{"columns":["col 1","col 2"], "index":["row 1","row 2"], "data":[["a","b"],["c","d"]]}' Encoding/decoding a Dataframe using 'records' formatted JSON.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-convert-pandas-dataframe-into-json-in-python
How to convert pandas DataFrame into JSON in Python? - GeeksforGeeks
June 12, 2025 - We are given a pandas DataFrame, ... human-readable format used for data exchange. With Pandas, this can be done easily using the to_json() method....
🌐
Pandas
pandas.pydata.org › docs › dev › reference › api › pandas.DataFrame.to_json.html
pandas.DataFrame.to_json — pandas documentation
orient='table' contains a ‘pandas_version’ field under ‘schema’. This stores the version of pandas used in the latest revision of the schema. ... >>> from json import loads, dumps >>> df = pd.DataFrame( ... [["a", "b"], ["c", "d"]], ... index=["row 1", "row 2"], ... columns=["col 1", "col 2"], ... ) >>> result = df.to_json(orient="split") >>> parsed = loads(result) >>> dumps(parsed, indent=4) {{ "columns": [ "col 1", "col 2" ], "index": [ "row 1", "row 2" ], "data": [ [ "a", "b" ], [ "c", "d" ] ] }}
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas read json file with examples
Pandas Read JSON File with Examples - Spark By {Examples}
January 10, 2025 - Let’s use pandas read_json() function to read JSON file into DataFrame. This by default supports JSON in single lines or in multiple lines.