
Research
/Security News
Malicious npm Packages Target WhatsApp Developers with Remote Kill Switch
Two npm packages masquerading as WhatsApp developer libraries include a kill switch that deletes all files if the phone number isn’t whitelisted.
simple-excel-to-json
Advanced tools
Read excel file and parse it to javascript Object.
npm install simple-excel-to-json
.parseXls2Json(path)
Where
var parser = require('simple-excel-to-json');
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 })
var parser = require('simple-excel-to-json');
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'
var parser = require('simple-excel-to-json');
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
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();
});
},
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
}
]
]
var parser = require('simple-excel-to-json');
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"
}
}
}
}
]
]
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,386 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
Two npm packages masquerading as WhatsApp developer libraries include a kill switch that deletes all files if the phone number isn’t whitelisted.
Research
/Security News
Socket uncovered 11 malicious Go packages using obfuscated loaders to fetch and execute second-stage payloads via C2 domains.
Security News
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.