Excel for JSON schema in Javascript

I have a task to create an excelsheet form, where I have to create the form according to the data type presented in excelsheet. Example:
enter image description here

I am trying to make a diagram JSONon top of excel data so that I can insert it into mongodb to dynamically create the form.

The following is the code I'm trying to implement:

            var workbook = XLSX.readFile(req.file.path);
            //console.log(workbook);
            var result = {};
            workbook.SheetNames.forEach(function (sheetName) {
                var roa = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
                if (roa.length > 0) {
                    result = roa;
                }
            });
            //return result;
            //console.log(result);

            var jsonData = {};
            var dropdown = {};
            var attrTypes = result[0];
            //console.log(attrTypes);

            for (var i = 1; i < result.length; i++) {
                var obj = result[i];
                //console.log(obj);
                for (var key in obj) {
                    var attrName = key;
                    var attrValue = obj[key];
                    if (attrTypes[attrName]) {
                        var type = attrTypes[attrName].toLowerCase().replace(/ /g, ''); // Means type is given                        
                        //console.log(type);

                        if (type === "selectbox") {
                            console.log(attrValue);
                            //var dropdown = attrValue;
                            //console.log(dropdown);
                        }

                    } else {
                        //console.log(type); // Means type is not given
                        jsonData = attrName + ":" + attrValue;
                        //console.log(jsonData);
                    }
                }
            }

Expected Conclusion JSON:

[
{
    Number : 1,
    FirstName : "Abc",
    LastName : "Xyza",
    Dept: ['Finance','Health','Insurance'],
    Country : ['US','Australia','Canada'],
    Year : ['2014','2015','2016'],,
    DateofBirth" : new Date(1937,05,02),
    Gender : ['M','F']    
},
{
    Number : 2,
    FirstName : "Abcd",
    LastName : "Xyzb",
    Dept: ['Finance','Health','Insurance'],
    Country : ['US','Australia','Canada'],
    Year : ['2014','2015','2016'],,
    DateofBirth" : new Date(1948,10,27),
    Gender : ['M','F']    
}
        .
        .
        and so on
]

Above is the code I'm trying to implement in MEANSTACK.

Any help would be appreciated.

+4
source share
3 answers

You can read XLSX on the client side and other excel formats using JS-XLSX .

, , , . . ,

[   
    {  
      "Number":1,
      "FirstName":"Abc",
      "LastName":"Xyza",
      "Dept":"Finance",
      "Country":"US",
      "Year":2014,
      "DateOfBirth":19370502,
      "Gender":"M"
    },
    {  
      "Number":2,
      "FirstName":"Abcd",
      "LastName":"Xyzb",
      "Dept":"Health",
      "Country":"Australia",
      "Year":2014,
      "DateOfBirth":19481027,
      "Gender":"F"
    }
]

, :

{  
   "Dept":{  
      "type":"dropdown",
      "values":[  
         "Finance",
         "Health",
         "Insurance"
      ]
   },
   "Country":{  
      "type":"dropdown",
      "values":[  
         "US",
         "Australia",
         "Canada"
      ]
   },
   "Year":{  
      "type":"dropdown",
      "values":[  
         2014,
         2015,
         2016
      ]
   },
   "Gender":{  
      "type":"radio button",
      "values":[  
         "M",
         "F"
      ]
   }
}

//included single objects from both for brevity
jsonSchema = {
    array: [
        {  
            "Number":2,
            "FirstName":"Abcd",
            "LastName":"Xyzb",
            "Dept":"Health",
            "Country":"Australia",
            "Year":2014,
            "DateOfBirth":19481027,
            "Gender":"F"
        }
    ],
    inputs: {
        "Gender":{  
            "type":"radio button",
            "values":[  
                "M",
                "F"
            ]
        }
    }
};

. JSON. Date

JSON GIT

https://github.com/ConsciousObserver/stackoverflow/tree/master/excelTest

.

Generated forms

JSON.

{
  "array": [
    {
      "Number": 1,
      "FirstName": "Abc",
      "LastName": "Xyza",
      "Dept": "Finance",
      "Country": "US",
      "Year": 2014,
      "DateOfBirth": 19370502,
      "Gender": "M"
    },
    {
      "Number": 2,
      "FirstName": "Abcd",
      "LastName": "Xyzb",
      "Dept": "Health",
      "Country": "Australia",
      "Year": 2014,
      "DateOfBirth": 19481027,
      "Gender": "F"
    },
    {
      "Number": 3,
      "FirstName": "Abce",
      "LastName": "Xyzc",
      "Dept": "Health",
      "Country": "US",
      "Year": 2015,
      "DateOfBirth": 19441029,
      "Gender": "F"
    },
    {
      "Number": 4,
      "FirstName": "Abcf",
      "LastName": "Xyzd",
      "Dept": "Insurance",
      "Country": "Canada",
      "Year": 2016,
      "DateOfBirth": 19481030,
      "Gender": "M"
    },
    {
      "Number": 5,
      "FirstName": "Abcg",
      "LastName": "Xyze",
      "Dept": "Finance",
      "Country": "Canada",
      "Year": 2016,
      "DateOfBirth": 19480604,
      "Gender": "M"
    }
  ],
  "inputs": {
    "Dept": {
      "type": "dropdown",
      "values": [
        "Finance",
        "Health",
        "Insurance"
      ]
    },
    "Country": {
      "type": "dropdown",
      "values": [
        "US",
        "Australia",
        "Canada"
      ]
    },
    "Year": {
      "type": "dropdown",
      "values": [
        2014,
        2015,
        2016
      ]
    },
    "Gender": {
      "type": "radio button",
      "values": [
        "M",
        "F"
      ]
    }
  }
}
+1

JSON XLSX. .

Java. apache.poi XLSX mongodb.bson JSON. , .

Javascript, github, .

0

, . Abc Xyza, , . , , Excel, , .

: http://json-schema.org/examples.html

So, if the title of the question is correct and you need a JSON scheme, then I would go for functions that create an array of strings from the provided values, in the case of dropdown or radio exchange types, determining the data type of the values ​​in the columns (int for number, string for FirstName, etc.), defining the minimum and maximum values ​​or even the allowed string patterns.

The result that I present here looks something like this:

{
    "id" : "http://your.site/form-schema",
    "title" : "Form schema",
    "description" : "JSON schema for autogenerating forms",
    "type" : "object",
    "properties" : {
        "Number" : {
            "type" : "integer"
        },
        "FirstName" : {
            "type" : "string"
        },
        "LastName" : {
            "type" : "string"
        },
        "Dept" : {
            "type" : "string",
            "oneOf" : [
                        { "format" : "Finance"},
                        { "format" : "Health" },
                        { "format" : "Insurance" }
            ]
        },
        "Country" : {
            "type" : "string",
            "oneOf" : [
                        {"format" : "US" },
                        { "format" : "Australia" },
                        { "format" : "Canada" }
            ]
        },
        "Year" : {
            "type" : "integer",
            "oneOf" : [
                        { "format" : "2014" },
                        { "format" : "2015" },
                        { "format" : "2016" }
            ]
        },
        "DateofBirth" : {
            "type" : "string",
            "pattern" : "yyyyMMdd"
        },
        "Gender" : {
            "enum" : ["M", "F"]
        }
    },
    "required" : ["Number", "FirstName", "LastName"],
    "additionalProperties" : false
}
0
source

All Articles