pythonasposeaspose-cells

how to add customized fields before saving the data to json


I have a simple python program which converts excel to json .

import  jpype     
import  asposecells     
jpype.startJVM() 
from asposecells.api import Workbook
workbook = Workbook("sample.xlsx")
workbook.save("sample3.json")
jpype.shutdownJVM()

And i get the output as below

[
 {
  "Name": "DS",
  "Gender": "M"
 },
{
  "Name": "DS1",
  "Gender": "M"
 },
]

Instead i want the output to have some extra (hardcoded) fields and word data appended

[
 {
  "date": "06/05/2022",
  "data":{
    "Name": "DS",
    "Gender": "M"
    }
 },
 {
  "date": "06/05/2022",
  "data":{
   "Name": "DS1",
   "Gender": "M"
  }
 },
]

Any insights on aspose-cells ? i referred the aspose docs , but didnt find anyways to do it. Any help will be appreciated. Thanks in advance.


Solution

  • Instead of saving it to a json, convert it to a python object, modify it and save. If you cannot use that library to create a json string/python object, you can load that file with the json library and make the necessary changes:

    import json
    json_list = json.load("sample3.json")
    new_list = []
    
    for l in json_list:
        #do_whatever, for example...
        new_list.append({"data": l, "date": "1-1-2020"})
    
    with open('sample3_extended.json', 'w') as outfile:
        json.dump(new_list, outfile)
    

    The formatting of your hardcoded example is wrong, I guess you want a list of dictionaries like this:

    [
     {
      "date": "06/05/2022",
      "data":{
        "Name": "DS",
        "Gender": "M"
        }
     },
     {
      "date": "06/05/2022",
      "data":{
       "Name": "DS1",
       "Gender": "M"
      }
     },
    ]