user1179317
user1179317

Reputation: 2903

Pandas to_json in separate lines

How can I get the json format from pandas, where each rows are separated with new line. For example if I have a dataframe like:

import pandas as pd

data = [{'a': 1, 'b': 2},
        {'a': 3, 'b': 4}]

df = pd.DataFrame(data)


print("First:\n", df.to_json(orient="records"))

print("Second:\n", df.to_json(orient="records", lines=True))

Output:

First:
 [{"a":1,"b":2},{"a":3,"b":4}]
Second:
 {"a":1,"b":2}
{"a":3,"b":4}

But I really want an output like so:

[{"a":1,"b":2},
{"a":3,"b":4}]

or

[
 {"a":1,"b":2},
 {"a":3,"b":4}
]

I really just want each line to be separated by new line, but still a valid json format that can be read. I know I can use to_json with lines=True and just split by new line then .join, but wondering if there is a more straight forward/faster solution just using pandas.

Upvotes: 0

Views: 1925

Answers (3)

Pedro Maia
Pedro Maia

Reputation: 2722

Why don't you just add the brackets:

print(f"First:\n[{df.to_json(orient='records', lines=True)}]")
print(f"Second:\n[\n{df.to_json(orient='records', lines=True)}\n]")

Upvotes: 1

Amir Aref
Amir Aref

Reputation: 401

Use indent parameter

import pandas as pd

data = [{'a': 1, 'b': 2},
        {'a': 3, 'b': 4}]

df = pd.DataFrame(data)

print(df.to_json(orient="records", indent=1))
#output :
[
 {
  "a":1,
  "b":2
 },
 {
  "a":3,
  "b":4
 }
]

Upvotes: 2

Saravanan Natarajan
Saravanan Natarajan

Reputation: 375

Here you go:

import json

list(json.loads(df.to_json(orient="index")).values())

Upvotes: 2

Related Questions