0

I have a small requirement where in I need to list down all customers and their amounts for a given fortnight in a JSON format.Currently, I have a dataframe this way -

  FortNight       Amount      Customer     Parameter
  Apr-2FN-2018   339632.00    10992     CustomerSales
  Apr-2FN-2018   27282.00     10994     CustomerSales 
  Apr-2FN-2018   26353.00     10995     CustomerSales 
  May-1FN-2018   24797.00     11000     CustomerSales
  May-1FN-2018   21093.00     10990     CustomerSales
  May-2FN-2018   45679.0      10992     CustomerSales
  May-2FN-2018   45459.0      10995     CustomerSales
  May-2FN-2018   35885.0      10994     CustomerSales

Expected JSON-

"CustomerSales" : [                                                                
    {"FortNight" : "Apr-2FN-2018",                                                                                      
         "Details" :[
             {"Customer":  "10992","Amount" : 339632.00},                                                                                                                                
             {"Customer":  "10994","Amount" : 27282.00},
             {"Customer":  "10995","Amount" : 26353.00}
           ]
    },
    {"FortNight" : "May-1FN-2018",                                                                                      
         "Details" :[
             {"Customer":  "11000","Amount" : 24797.00},                                                                                                                                
             {"Customer":  "10990","Amount" : 21093.00}
           ]
    },
    {"FortNight" : "May-2FN-2018",                                                                                      
         "Details" :[
             {"Customer":  "10992","Amount" : 45679.00},                                                                                                                                
             {"Customer":  "10995","Amount" : 45459.00},
             {"Customer":  "10994","Amount" : 35885.00}
           ]
    }
]

I tried the logic from - Convert dataframe to JSON with 2 level nested array

But I am still stuck at another level more.

I tried other ways too, but in vain. Any help is welcome. Thanks in advance!!...

  • What methods did not work for you? – mad_ Jul 12 '18 at 15:02
  • Currently I could get the output as below- [{'Customersales': [{'FortNight': 'Apr-2FN-2018', 'Details': [{'Customer': '1100', 'Amount': 198218.02}]}]}, {'Customersales': [{'FortNight': 'Apr-3FN-2018', 'Details': [{'Customer': '1100', 'Amount': 198218.02}]}]}, {'Customersales': [{'FortNight': 'May-1FN-2018', 'Details': [{'Customer': '1100', 'Amount': 198218.02}]}]}] – Sparsha Devapalli Jul 12 '18 at 15:27

1 Answers1

0

You could make this easier by using a dictionary of CustomerSales where the key is equal to the FortNight value instead of having a list of dictionaries using a "FortNight" string as a key in each dictionary. Given the requirements specified the following should work. You can loop through the DataFrame and build out a dictionary. Then the second set of code converts that dictionary into the format you specified.

sales = {}

for idx, row in df.iterrows():
    fn = row["FortNight"]
    fn_sales = sales.get(fn)
    if fn_sales is None:
        sales[fn] = {
                "Details": []
                }
        fn_sales = sales.get(fn)

    fn_sales["Details"].append({
            "Customer": row["Customer"],
            "Amount": row["Amount"]
            }
    )

sales_obj = {"CustomerSales": []}

for fn, details in sales.items():
    sales_obj["CustomerSales"].append({
            "FortNite": fn,
            "Details": details["Details"]
            })
vielkind
  • 2,485
  • 1
  • 13
  • 16