0

I'm exporting a dataframe to a JSON file, through these lines of code:

with open('example.json', 'w') as f:
for row in df3.iterrows():
    row[1].to_json(f, orient=None, lines=False)
    f.write("\n") 

And it returns a file like this:

{"age":20,"city":"Burdinne","email":"[email protected]","name":"Zorita","phone":4565434645.0,"postal_code":42680.0,"regDate":"2015-06-14T12:12:00-07:00"}
{"age":22,"city":"Bharatpur","email":"[email protected]","name":"Mariam","phone":null,"postal_code":null,"regDate":"2016-10-14T18:52:48-07:00"}
{"age":28,"city":"Neerheylissem","email":"[email protected]","name":"Malik","phone":null,"postal_code":null,"regDate":"2016-09-20T18:06:55-07:00"}
{"age":24,"city":"San Fratello","email":"[email protected]","name":"Claire","phone":null,"postal_code":null,"regDate":"2016-12-29T09:49:13-08:00"}
{"age":30,"city":"La Cruz","email":"[email protected]","name":"Hilel","phone":null,"postal_code":null,"regDate":"2016-07-09T12:03:31-07:00"}

However, I would like that JSON file to be tabulated like this:

[
  {
    "name": "Zorita",
    "email": "[email protected]",
    "regDate": "2015-06-14T12:12:00-07:00",
    "city": "Burdinne",
    "age": 20,
    "postal_code":42680,
    "phone": 4565434645
  },
  {
    "name": "Mariam",
    "email": "[email protected]",
    "regDate": "2016-10-14T18:52:48-07:00",
    "city": "Bharatpur",
    "age": 22
  },
  {
    "name": "Malik",
    "email": "[email protected]",
    "regDate": "2016-09-20T18:06:55-07:00",
    "city": "Neerheylissem",
    "age": 28
  },
  {
    "name": "Claire",
    "email": "[email protected]",
    "regDate": "2016-12-29T09:49:13-08:00",
    "city": "San Fratello",
    "age": 24
  },
  {
    "name": "Hilel",
    "email": "[email protected]",
    "regDate": "2016-07-09T12:03:31-07:00",
    "city": "La Cruz",
    "age": 30
  }
]

How could I do this? In my code I'm trying to put the line break with "\ n" but apparently I'm not doing it correctly

6
  • The for loop should be with proper indentation. Commented Mar 8, 2019 at 8:20
  • Can u show your dataframe? Commented Mar 8, 2019 at 8:22
  • Then how should I modify it? Commented Mar 8, 2019 at 8:22
  • The dataframe comes from a CSV File Commented Mar 8, 2019 at 8:23
  • Use the json library - it has a dump function with an indent parameter that will pretty-print the code for you. Commented Mar 8, 2019 at 8:29

3 Answers 3

1

Try below code:

final_list = list()
for row in df3.iterrows():
    final_list.append(row[1].to_dict(orient=None))

with open('example.json', 'w') as f:
    f.write(json.dumps(final_list, indent=4))
Sign up to request clarification or add additional context in comments.

5 Comments

With the code the result is similar to this: "[{\"age\":20,\"city\":\"Burdinne\",\"email\":\"[email protected]\",\"name\":\"Zorita\.." and in a single line
You need indentation for not a single line output. Updated solution @IriSandivel
These diagonals keep appearing: "\" [ "{\"age\":20,\"city\":\"Burdinne\",\"email\":\"[email protected]\",\"name\":\"Zorita\",\"phone\":4565434645.0,\"postal_code\":42680.0,\"regDate\":\"2015-06-14T12:12:00-07:00\"}", ]
These quotes are arriving due to double encoding of JSON strings. Try to remove to _json while appending in final_list. Updated solution @IriSandivel
I just had to remove "orient=None". But it's ready. Thanks!!
1

You can convert column to list and write to file by json.dump with parameter indent and if necessary sort_keys=True for pretty json:

import json

with open("example.json", "w") as f:
    json.dump(df[1].tolist(), f, indent=4, sort_keys=True)

Sample:

d = [
  {
    "name": "Zorita",
    "email": "[email protected]",
    "regDate": "2015-06-14T12:12:00-07:00",
    "city": "Burdinne",
    "age": 20,
    "postal_code":42680,
    "phone": 4565434645
  },
  {
    "name": "Mariam",
    "email": "[email protected]",
    "regDate": "2016-10-14T18:52:48-07:00",
    "city": "Bharatpur",
    "age": 22
  }

]

df = pd.DataFrame({1: d})
#print (df)

import json

with open("example.json", "w") as f:
    json.dump(df[1].tolist(), f, indent=4, sort_keys=True)

[
    {
        "age": 20,
        "city": "Burdinne",
        "email": "[email protected]",
        "name": "Zorita",
        "phone": 4565434645,
        "postal_code": 42680,
        "regDate": "2015-06-14T12:12:00-07:00"
    },
    {
        "age": 22,
        "city": "Bharatpur",
        "email": "[email protected]",
        "name": "Mariam",
        "regDate": "2016-10-14T18:52:48-07:00"
    }
]

Comments

0

Although this has been answered by @skaul05, using iterrows can be inefficient. This might be better

with open('file.json', 'w') as f:
    f.write(json.dumps(json.loads(df.to_json()), indent=4))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.