<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>fforw.de &#187; NodeJS</title>
	<atom:link href="http://fforw.de/tag/nodejs/feed/" rel="self" type="application/rss+xml" />
	<link>http://fforw.de</link>
	<description>skip the boring parts</description>
	<lastBuildDate>Sat, 12 Nov 2011 11:54:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Scripting JSON</title>
		<link>http://fforw.de/post/scripting-json/</link>
		<comments>http://fforw.de/post/scripting-json/#comments</comments>
		<pubDate>Sun, 16 May 2010 15:51:14 +0000</pubDate>
		<dc:creator>fforw</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[NodeJS]]></category>

		<guid isPermaLink="false">http://fforw.de/?p=158</guid>
		<description><![CDATA[Doing a lot of web stuff and fiddling around with CouchDB, I really got to like JSON as versatile format for things. Installing the JSONView extension for firefox really helps with working with JSON in the browser, but what I&#8217;ve been missing so far is an easy way to deal with JSON from bash scripts. [...]]]></description>
			<content:encoded><![CDATA[<p>Doing a lot of web stuff and fiddling around with CouchDB, I really got to like JSON as versatile format for things. Installing the<a href="https://addons.mozilla.org/en-US/firefox/addon/10869/"> JSONView extension for firefox</a> really helps with working with JSON in the browser, but what I&#8217;ve been missing so far is an easy way to deal with JSON from bash scripts. Fiddling around with the very interesting <a href="http://nodejs.org/">NodeJS</a>, I came up with a small node js script that makes JSON handling much easier, the JSON command: It reads a JSON object from stdin and feeds it to a javascript function body with &#8220;v&#8221; and NodeJS&#8217; &#8220;sys&#8221; as parameters. The return value of the function is written to stdout. If it was a string, it is written as-is, if it is another object it will be pretty-JSONified.</p>
<h2>Simple Example</h2>
<pre>$ curl -s http://localhost:5984/test | json "return v;"
{
 "db_name": "test",
 "doc_count": 0,
 "doc_del_count": 0,
 "update_seq": 0,
 "purge_seq": 0,
 "compact_running": false,
 "disk_size": 79,
 "instance_start_time": "1274021449672284",
 "disk_format_version": 5
}</pre>
<p>Use curl to fetch the status of CouchDB database &#8220;test&#8221; from the local CouchDB node and then just pretty print it by returning the implicit value v.</p>
<pre>$ curl -s http://localhost:5984/test | json "return v.disk_size;"
79
</pre>
<p>Just print the disk_size of CouchDB database &#8220;test&#8221;. You can use all the modern JavaScript functions v8 offers plus the implicit &#8220;sys&#8221; object that lets you log stuff to stderr or inspect objects. A little script that I find highly useful:</p>
<pre>#!/bin/bash
# Delete all jcouchdb test databases
DBS=$(curl -s http://localhost:5984/_all_dbs | \
json 'return v.filter( function(db) { return db.indexOf("jcouchdb") == 0; }).join("\n");')

for i in $DBS
do
 curl -X DELETE http://localhost:5984/$i
done
</pre>
<p>Filter the list of database to only contain those that start with &#8220;jcouchdb&#8221;, then loop over them to delete.</p>
<p><strong>Links:</strong></p>
<ul>
<li><a href="http://fforw.de/static/files/json">&#8220;json&#8221; command line script</a>. Requires NodeJS to be installed in /usr/local. If it&#8217;s installed somewhere else, you need to change the script</li>
</ul>
<p><small><strong>Update: </strong>Added &#8220;return v;&#8221; as default function. now also supports &#8220;-h&#8221; and &#8220;&#8211;help&#8221;.</small></p>
]]></content:encoded>
			<wfw:commentRss>http://fforw.de/post/scripting-json/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

