Skip to content

Commit

Permalink
+=json2jl
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Murphy-Skvorzov committed Nov 26, 2015
1 parent 1e483bb commit 6f39381
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
46 changes: 46 additions & 0 deletions bin/json2jl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env node

var opt = require('../lib/opt').fancy({filename:__filename, usage:
[ 'Usage: jline-json2jl <--help|--version>'
, ' jline-json2jl <filename>'
, ' jline-json2jl -a <array_path> <filename>'
, ' jline-json2jl -d <dict_path> <filename>'
].join("\n")});

console.log(opt);

var parsePath = require('./parsePath')
, fs = require('fs');

function getPath(thing, path){
return path.reduce(function(thing,bit){return thing && thing[bit];},thing);
}

var data = JSON.parse(fs.readFileSync(opt.filename));
if (opt.array_path){
var path = parsePath(opt.array_path);
var array = getPath(data,path);
if (!array){
console.error("ERROR: Array missing");
process.exit(1);
}
if (!array.map){
console.error("ERROR: Not an array:\n ", JSON.stringify(array).substr(0,10)+"...");
process.exit(2);
}
array.forEach(function(d){console.log(JSON.stringify(d));});
} else if (opt.dict_path){
var path = parsePath(opt.dict_path);
var dict = getPath(data,path);
if (!dict){
console.error("ERROR: Dict missing");
process.exit(1);
}
if ('object' !== typeof(dict)){
console.error("ERROR: Not a dictionary:\n ", JSON.stringify(array).substr(0,10)+"...");
process.exit(2);
}
Object.keys(dict).forEach(function(k){console.log(JSON.stringify({key:k,val:dict[k]}));});
} else {
console.log(JSON.stringify(data));
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jline",
"version": "0.0.42",
"version": "0.0.43",
"description": "Sort, grep and join files where every line is JSON.",
"main": "index.js",
"bin": {
Expand All @@ -24,6 +24,7 @@
"jline-sort": "bin/sort.js",
"jline-csv2jl": "bin/csv2jl.js",
"jline-csv2jla": "bin/csv2jla.js",
"jline-json2jl": "bin/json2jl.js",
"jline-mysql2jl": "bin/mysql2jl.sh",
"jline-mysql2jla": "bin/mysql2jla.sh",
"jline-numper": "util/numper.js",
Expand Down

0 comments on commit 6f39381

Please sign in to comment.