Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
deepjson2csv
Advanced tools
Readme
This project continues zeMirco's jso2csv module, with the deep json feature. Ex: fields: ['info.firstName', 'info.lastName', 'info.email']
Converts json files into csv with column titles and proper line endings. Can be used as a module and from the command line.
Install
$ npm install deepjson2csv
Include the module and run
var deepjson2csv = require('deepjson2csv');
deepjson2csv({data: someJSONData, fields: ['field1', 'field2', 'field3']}, function(err, csv) {
if (err) console.log(err);
console.log(csv);
});
var deepjson2csv = require('deepjson2csv');
var json = [
{
"car": "Audi",
"price": 40000,
"color": "blue"
}, {
"car": "BMW",
"price": 35000,
"color": "black"
}, {
"car": "Porsche",
"price": 60000,
"color": "green"
}
];
deepjson2csv({data: json, fields: ['car', 'price', 'color']}, function(err, csv) {
if (err) console.log(err);
fs.writeFile('file.csv', csv, function(err) {
if (err) throw err;
console.log('file saved');
});
});
The content of the "file.csv" should be
car, price, color
"Audi", 40000, "blue"
"BMW", 35000, "black"
"Porsche", 60000, "green"
Similarly to mongoexport you can choose which fields to export
deepjson2csv({data: json, fields: ['car', 'color']}, function(err, csv) {
if (err) console.log(err);
console.log(csv);
});
Results in
car, color
"Audi", "blue"
"BMW", "black"
"Porsche", "green"
Use a custom delimiter to create tsv files. Add it as the value of the del property on the parameters:
deepjson2csv({data: json, fields: ['car', 'price', 'color'], del: '\t'}, function(err, tsv) {
if (err) console.log(err);
console.log(tsv);
});
Will output:
car price color
"Audi" 10000 "blue"
"BMW" 15000 "red"
"Mercedes" 20000 "yellow"
"Porsche" 30000 "green"
If no delimiter is specified, the default ,
is used
You can choose custom column names for the exported file.
deepjson2csv({data: json, fields: ['car', 'price'], fieldNames: ['Car Name', 'Price USD']}, function(err, csv) {
if (err) console.log(err);
console.log(csv);
});
Results in
"Car Name", "Price USD"
"Audi", "blue"
"BMW", "black"
"Porsche", "green"
deepjson2csv
can also be called from the command line
Usage: deepjson2csv [options]
Options:
-h, --help output usage information
-V, --version output the version number
-i, --input <input> Path and name of the incoming json file.
-o, --output [output] Path and name of the resulting csv file. Defaults to console.
-f, --fields <fields> Specify the fields to convert.
-l, --fieldList [list] Specify a file with a list of fields to include. One field per line.
-d, --delimiter [delim] Specify a delimiter other than the default comma to use.
-p, --pretty Use only when printing to console. Logs output in pretty tables.
An input file -i
and fields -f
are required. If no output -o
is specified the result is logged to the console.
Use -p
to show the result in a beautiful table inside the console.
$ deepjson2csv -i input.json -f carModel,price,color
carModel,price,color
"Audi",10000,"blue"
"BMW",15000,"red"
"Mercedes",20000,"yellow"
"Porsche",30000,"green"
$ deepjson2csv -i input.json -f carModel,price,color -p
$ deepjson2csv -i input.json -f carModel,price,color -o out.csv
Content of out.csv
is
carModel,price,color
"Audi",10000,"blue"
"BMW",15000,"red"
"Mercedes",20000,"yellow"
"Porsche",30000,"green"
The file fieldList
contains
carModel
price
color
Use the following command with the -l
flag
$ deepjson2csv -i input.json -l fieldList -o out.csv
Content of out.csv
is
carModel,price,color
"Audi",10000,"blue"
"BMW",15000,"red"
"Mercedes",20000,"yellow"
"Porsche",30000,"green"
$ deepjson2csv -f price
[{"price":1000},{"price":2000}]
Hit Enter and afterwards CTRL + D to end reading from stdin. The terminal should show
price
1000
2000
Requires mocha, should and async.
Run
$ make test
or
$ npm test
Requires js-beautify.
Run
$ make format
or
$ npm run-script format
Install require packages for development run following command under deepjson2csv dir.
Run
$ npm install
Could you please make sure code is formatted and test passed before submit Pull Requests?
See Testing and Formatting deepjson2csv above.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Convert deep json to csv with column titles
The npm package deepjson2csv receives a total of 33 weekly downloads. As such, deepjson2csv popularity was classified as not popular.
We found that deepjson2csv 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.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.