Skip to content

Instantly share code, notes, and snippets.

@sifu
Created November 3, 2010 14:27
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sifu/661132 to your computer and use it in GitHub Desktop.
Save sifu/661132 to your computer and use it in GitHub Desktop.
jsoneval
#!/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 ] );
} );
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 ) } );"
@wmertens
Copy link

wmertens commented Nov 3, 2010

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) );
    }
} );

@sifu
Copy link
Author

sifu commented Nov 4, 2010

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment