
Research
/Security News
Toptal’s GitHub Organization Hijacked: 10 Malicious Packages Published
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
simple-excel-to-json
Advanced tools
Read excel file and parse it to javascript Object.
npm install simple-excel-to-json
You can just require the modure "simple-excel-to-json" and execute the method 'parseXls2Json'
var parser = require('simple-excel-to-json')
var doc = parser.parseXls2Json('./example/sample8.xlsx');
This approach will use the same parser instance to convert a xls into the json data. If you have many xls which need different particular transform function, you can create many XlsParser instances then apply the transform functions(see basic usage and advance usage).
.parseXls2Json(path)
Where
// Create an instance for XlsParser
var parser = new (require('simple-excel-to-json').XlsParser)();
var doc = parser.parseXls2Json('./example/sample.xlsx');
//print the data of the first sheet
console.log(doc[0]);
item | price | number |
---|---|---|
apple | 100 | 2 |
banana | 200 | 12 |
coffee | 150 | 3 |
[
[
{
"item":"apple",
"price":100,
"number":2
},
{
"item":"banana",
"price":200,
"number":12
},
{
"item":"coffee",
"price":150,
"number":3
}
]
]
.parseXls2Json(path, { isNested: true })
// Create an instance for XlsParser
var parser = new (require('simple-excel-to-json').XlsParser)();
var doc = parser.parseXls2Json('./example/sample.xlsx', { isNested: true });
//print the data of the first sheet
console.log(doc[0]);
Type | Dealership.us[0].location | Dealership.us[1].location | Dealership.jp[0].location |
---|---|---|---|
Sedan | New York | Dallas | Tokyo |
SUV | Ohio | Osaka |
[
[
{
"type": "Sedan",
"dealership":
{
"us":
[
{
"location": "New York"
},
{
"location": "Dallas"
}
],
"jp":
[
{
"location": "Tokyo"
}
]
}
},
{
"type": "SUV",
"dealership":
{
"us":
[
{
"location": "Ohio"
},
{
"location": ""
}
],
"jp":
[
{
"location": "Osaka"
}
]
}
}
]
]
If you want to have output json with camel case properties, you can apply an 'option' { isToCamelCase: true } to parseXls2Json() e.g 'Car Name' to 'carName' 'product.Type.hasGPS' to 'product.type.hasGPS'
// Create an instance for XlsParser
var parser = new (require('simple-excel-to-json').XlsParser)();
var option =
{
isToCamelCase: true,
isNested: true,
}
var doc = parser.parseXls2Json('./example/sample6.xlsx', option );
price | product.Type.hasGPS | Model Name |
---|---|---|
100 | y | sedan 01 |
150 | y | SUV 22 |
200 | n | Sport Cars IV |
[
[
{
'price': 100,
'product':
{
'type':
{
'hasGPS': 'y'
}
},
'modelName': 'sedan 01'
},
{
'price': 150,
'product':
{
'type':
{
'hasGPS': 'y'
}
},
'modelName': 'SUV 22'
},
{
'price': 200,
'product':
{
'type':
{
'hasGPS': 'n'
}
},
'modelName': 'Sport Cars IV'
},
]
]
You can apply transfomation function to transform the output
.setTranseform(func)
Where
// Create an instance for XlsParser
var parser = new (require('simple-excel-to-json').XlsParser)();
parse.setTranseform( [
function(sheet1){
sheet1.number = sheet1.number.trim();
sheet1.buyer = sheet1.buyer.split(';').filter( item=>item.trim()!=='');
sheet1.buyer.forEach( (e,i,arr) => {
arr[i]=e.trim();
});
},
function(sheet2){
sheet2.Type = sheet2.Type.toLowerCase();
}
]);
var doc = parser.parseXls2Json('./example/sample2.xlsx');
item | price | number | buyer |
---|---|---|---|
apple | 100 | two | Andy;Bob |
banana | 200 | twelve | Tom; |
coffee | 150 | three | Mary; Calvin |
Type | Price |
---|---|
Car | 10000 |
Bus | 200000 |
[
[
{
"item":"apple",
"price":100,
"number":"two",
"buyer": ["Andy","Bob"]
},
{
"item":"banana",
"price":200,
"number":"twelve",
"buyer":["Tom"]
},
{
"item":"coffee",
"price":150,
"number":"three",
"buyer":["Mary","Calvin"]
}
],
[
{
"Type":"car",
"Price":10000
},
{
"Type":"bus",
"Price":20000
}
]
]
// Create an instance for XlsParser
var parser = new (require('simple-excel-to-json').XlsParser)();
parse.setTranseform( [
function(sheet1){
sheet1['type'] = sheet1['type'].toLowerCase();
sheet1['price'] = sheet1['price'].split(';').map( e => e.trim());
sheet1['dealership.us[0].location'] = sheet1['dealership.us[0].location'].trim();
},
]);
var doc = parser.parseXls2Json('./example/sample2.xlsx', { isNested: true });
type | price | dealership.us[0].location |
---|---|---|
Sedan | 2000;1000 | New York |
SUV | 2000;500 | Ohio |
[
[
{
"type": "sedan",
"price": ["2000","1000"],
"dealership":
{
"us":
[
{
"location": "new york"
}
]
}
},
{
"type": "suv",
"price": ["2000";"500"],
"dealership":
{
"us":
[
{
"location": "ohio"
}
}
}
}
]
]
If your sheet contains empty cell, simple-excel-to-json will give "" for this cell in result object.
Type | Price |
---|---|
Car | |
Bus | 200000 |
[
{
"Type":"car",
"Price":""
},
{
"Type":"bus",
"Price":20000
}
]
]
Type | Price | Dealership.us[0].location |
---|---|---|
Sedan | 2000 | |
SUV | 2000 | Ohio |
[
[
{
"type": "Sedan",
"price": 2000,
"dealership":
{
"us":
[
{
"location": ""
}
]
}
},
{
"type": "SUV",
"price": 2000,
"dealership":
{
"us":
[
{
"location": "ohio"
}
}
}
}
]
]
Support passing options to XLSX parser(node-xlsx) library
Fix vulnerability issue
Fix vulnerability issue
Fix bug
Export the constructor XlsParser that can be used to create multiple parser instances
Issue fix
Replace white space with '_' for property. for example, 'card number' in header --becomes--> 'card_number:'
Throw 'failedToTransformError' when failed to perform transformation
For example:
var parser = require('simple-excel-to-json');
parse.setTranseform( [
function(sheet1){
sheet1.number = sheet1.number.trim();
sheet1.buyer = sheet1.buyer.split(';').filter( item=>item.trim()!=='');
sheet1.buyer.forEach( (e,i,arr) => {
arr[i]=e.trim();
});
//lets throw an error for invalid array length
if(sheet1.buyer.length <1)
{
throw new Error('length of sheet1.buyer < 1 ')
}
},
function(sheet2){
sheet2.Type = sheet2.Type.toLowerCase();
}
]);
try
{
var doc = parser.parseXls2Json('./example/sample8.xlsx');
}
catch(err)
{
//capture the error
if(err instanceof parser.failedToTransformError)
{
console.log('name: '+ err.name)
console.log('message: '+ err.message)
console.log('stack: '+ err.stack)
}
}
//--------------output---------------
name: failedToTransformError
message: {"sheet number":0,"element":"{\"item\":\"banana\",\"price\":200,\"number\":\"twelve\",\"buyer\":[]}","index":0,"errorMessage":"length of sheet1.buyer < 1 "}
stack:
at C:\Users\andyl\Documents\excel-to-json\test\test.js:494:27
at C:\Users\andyl\Documents\excel-to-json\index.js:73:17
...
add a parameter 'option' to decide the output format
option = {
isNested: true,
toLowerCase: true
}
parseXls2Json(path, option)
isNested is true: convert excel data to nested json object toLowerCase is true: property in output json data will be lower case
Update README.md
Usage in README.md. Add example for Nested Case of Advance Usage
Support Nested JSON format
FAQs
parse excel to json object
The npm package simple-excel-to-json receives a total of 1,147 weekly downloads. As such, simple-excel-to-json popularity was classified as popular.
We found that simple-excel-to-json demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
Research
/Security News
Socket researchers investigate 4 malicious npm and PyPI packages with 56,000+ downloads that install surveillance malware.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.