0

I have a csv formatted like:

Column1,Column2,Column3
data1,data2,data3
data1,data2,data3

And I was able to convert it to json in this format:

[
  {
    "Column1": "data1",
    "Column2": "data2",
    "Column3": "data3",
  },
  {
    "Column1": "data1",
    "Column2": "data2",
    "Column3": "data3",
  },
]

with this code:

import pandas as pd
import json

df = pd.read_csv(r'ReactStepsSubsteps.csv', encoding='windows-1254')

newList = []
for index, item in df.iterrows():
    newDict = {}
    for key in item.keys():
        newDict[key] = item[key]

    newList.append(newDict)

with open('data.json', 'w', encoding='utf-8') as f:
    json.dump(newList, f, ensure_ascii=False, indent=4)

Is there a way to do this exclusively with helper functions from pandas?

pianoman102
  • 541
  • 8
  • 22

2 Answers2

2

The pandas documentation for to_json list several orientations the output data can have.

import pandas as pd
df = pd.DataFrame({'Column1': {0: 'data1', 1: 'data1'},
 'Column2': {0: 'data2', 1: 'data2'},
 'Column3': {0: 'data3', 1: 'data3'}})

df.to_json(orient='records')

Output

[
    {
        "Column1":"data1",
        "Column2":"data2",
        "Column3":"data3"
    },
    {
        "Column1":"data1",
        "Column2":"data2",
        "Column3":"data3"
    }
]
Chris
  • 15,819
  • 3
  • 24
  • 37
0

Try this:

newList = df.to_json(orient="records")
Rabinzel
  • 7,757
  • 3
  • 10
  • 30