pythonjsonpandasdataframeto-json

DataFrame to JSON giving absurd output


I have this code

    eventSummary = {}
    eventSummary['jobClassID'] = data['jobClassID'].split()
    eventSummary['modelID'] =data['modelID'].split()
    eventSummary['startDate'] = pd.to_datetime(start).date().strftime("%Y-%m-%d").split()
    eventSummary['hourlySummary']=merged_df.to_json(orient = 'records')
    dataF = pd.DataFrame(eventSummary)
    dataF.to_json(path_or_buf = '*\merged2.json', orient="records")

which is giving me json like this :

{

"jobClassID":"NorthernRegion|FarNorthDistrict|GeneralDuties|Constable|FIR|Senior|Lead|5",
"modelID":"4",
"startDate":
"2019-01-01",
"hourlySummary":
"[{\"Day\":1,\"Hour\":0,\"Month\":1,\"priority\":3,\"Count\":3,\"Hourly Season\":1.5589041096,\"Weekly Season\":1.9205974843,\"Monthly Season\":2.0497311828,\"Noise Factor\":0.6517894485,\"groupId\":0,\"K (Service)\":0.6955185685,\"Theta (Service)\":10924.8330481261,\"K (Travel)\":0.6819906983,\"Theta (Travel)\":5694.9711557023}]

}

But I want output to be like this: {

"jobClassID":["NorthernRegion|FarNorthDistrict|GeneralDuties|Constable|FIR|Senior|Lead|5"],
"modelID":["4"],
"startDate":["2019-01-01"],
"hourlySummary":[
                  {"Day":1,"Hour":0,"Month":1,"priority":3,"Count":3,"Hourly Season":1.5589041096,"Weekly Season":1.9205974843,"Monthly Season":2.0497311828,"Noise Factor":0.6517894485,"groupId":0,"K (Service)":0.6955185685,"Theta (Service)":10924.8330481261,"K (Travel)":0.6819906983,"Theta (Travel)":5694.9711557023}]

}

How to achieve this?

Thanks in advance.


Solution

  • eventSummary['jobClassID'] = [data['jobClassID'].split()]
    eventSummary['modelID'] = [data['modelID'].split()]
    eventSummary['startDate'] = [pd.to_datetime(start).date().strftime("%Y-%m- 
     %d").split()]
    eventSummary['hourlySummary']= [merged_df.to_dict(orient='records')]
    dataF = pd.DataFrame(eventSummary)
    dataF.to_json(path_or_buf = '*\merged12.json', orient="records", lines = True)
    

    This code is giving results as expected.

    Thanks for your efforts, everyone.