Skip to content

Commit

Permalink
Solidify json2jl
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Murphy-Skvorzov committed Nov 26, 2015
1 parent 6f39381 commit 93048d7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ Use `--help` or refer to the markdown files:
### Input
* [jline-mysql2jl](bin/mysql2jl.md) - EXPERIMENTAL - mysql output to JSON dictionaries
* [jline-mysql2jla](bin/mysql2jla.md) - EXPERIMENTAL - mysql output to JSON arrays
* [jline-json2jl](bin/json2jl.md) - EXPERIMENTAL - JSON to JSON lines.
52 changes: 35 additions & 17 deletions bin/json2jl.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,53 @@
#!/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>'
[ 'Usage: jline-json2jl <filename> [--path=<path>]'
, ' jline-json2jl -a <filename> [--path=<path>]'
, ' jline-json2jl -d <filename> [--path=<path>]'
, ' jline-json2jl -k <filename> [--path=<path>]'
, ' jline-json2jl --help'
, ' jline-json2jl --version'
].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);
var data = JSON.parse(fs.readFileSync(opt["<filename>"]));
var path = parsePath(opt["<path>"]||'');
var selection = getPath(data,path);

if (opt["-a"]){
var array = selection;
if (!array){
console.error("ERROR: Array missing");
process.exit(1);
}
if (!array.map){
if ('object' !== typeof(array)){
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);
// Should be an array. May be a dictionary, in which case we print just the values.
if (Array.isArray(array)) array.forEach(function(d){console.log(JSON.stringify(d));});
else Object.keys(array).forEach(function(k){console.log(JSON.stringify(array[k]));});
} else if (opt["-d"]){
var dict = selection;
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);
}
// Should be a dict. May be an array, in which case the indices are used as keys.
if (Array.isArray(array)) array.forEach(function(d,i){console.log(JSON.stringify({key:i,val:d}));});
else Object.keys(dict).forEach(function(k){console.log(JSON.stringify({key:k,val:dict[k]}));});
} else if (opt["-k"]){
var dict = selection;
if (!dict){
console.error("ERROR: Dict missing");
process.exit(1);
Expand All @@ -40,7 +56,9 @@ if (opt.array_path){
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]}));});
// Should be a dict. May be an array, in which case the indices are used as keys.
if (Array.isArray(array)) array.forEach(function(d,i){console.log(i);});
else Object.keys(dict).forEach(function(k){console.log(JSON.stringify(k));});
} else {
console.log(JSON.stringify(data));
console.log(JSON.stringify(selection));
}
17 changes: 17 additions & 0 deletions bin/json2jl.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
jline-json2jl
=============

Convert a JSON file into JSON lines

## Command line:

jline-json2jl data.json # Emit the entire JSON on one line
jline-json2jl -a data.json # If the JSON is an array, print each entry as a JSON line.
# If the JSON is a dictionary, print each value.
jline-json2jl -a --path=foo.bar package.json
# Get an array from inside the JSON and
# print each entry as a line.
# --path may be used with anything.
jline-json2jl -d data.json # Print each key value pair as a JSON line. {key:.., val:..}
jline-json2jl -k data.json # Print the keys of a dictionary.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jline",
"version": "0.0.43",
"version": "0.0.44",
"description": "Sort, grep and join files where every line is JSON.",
"main": "index.js",
"bin": {
Expand Down

0 comments on commit 93048d7

Please sign in to comment.