public
Last active

jsoneval

  • Download Gist
USAGE.txt
1 2 3
eg. to print the ids of all documents within a couchdb database:
 
curl "http://127.0.0.1:5984/somedb/_all_docs" | jsoneval.js "body.rows.forEach( function( r ){ console.info( r.id ) } );"
jsoneval.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12
#!/usr/bin/env node
 
var stdin = process.openStdin( );
stdin.setEncoding( 'utf8' );
var chunks = '';
stdin.on( 'data', function ( chunk ) {
chunks += chunk.toString( 'utf8' );
} );
stdin.on('end', function () {
var body = JSON.parse( chunks );
eval( process.argv[ 2 ] );
} );

I changed it like this to pretty-print if there is no argument:

#!/usr/bin/env node
// Use like this:
// curl "http://127.0.0.1:5984/somedb/_all_docs" | jsoneval.js "body.rows.forEach( function( r ){ console.info( r.id ) } );"

var stdin = process.openStdin( );
stdin.setEncoding( 'utf8' );
var chunks = '';
stdin.on( 'data', function ( chunk ) {
    chunks += chunk.toString( 'utf8' );
} );
stdin.on('end', function () {
    var body = JSON.parse( chunks );
    if ( process.argv[ 2 ] ) {
        eval( process.argv[ 2 ] );
    } else {
        console.info( JSON.stringify(body, null, 4) );
    }
} );

thank you! nice idea, but if i just want to pretty-print something i just pipe it through:
"python -mjson.tool"
which i aliased to pp

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.