News for hackers. Stuff that matters.
Re:Jshon: A command line program to parse, read and create JSON by arethuza on Friday February 18, 2011 @05:19AM (#2234940)
I've been using CouchDB a lot over the last six months or so and I've been seeing a lot of JSON on the command line - either through curl or using my own application-specific command line utilities. I must admit that I rather like working with JSON on the command line - it is easy on the eye but allows far more powerful structures than the usual Unix approach of lines with delimited fields (which I've been happily using for 20+ years).

Using JSON to extend command line processing looks to me like it is a nice step forward. Things like Microsoft's PowerShell are perhaps conceptually more elegant (where you pass real live objects about) but the practical reality always seems very fussy and tied to a particular programming platform. Other approaches (e.g. passing XML between Unix-style command line programs) isn't very appealing due to the general ugliness and verbosity of most XML.

Re:Jshon: A command line program to parse, read and create JSON by forgotusername on Friday February 18, 2011 @03:56AM (#2234800)
Note for simple uses, Python 2.6+ comes with the "json.tool" module (also part of simplejson if you're <= 2.5), which reads JSON on stdin (or argv[1]) before pretty printing it.

    $ type json
    json is aliased to `python -m json.tool'
    $ curl -s twitter.com/users/BarackObama.json|json|grep '"url"'
        "url": "http://www.barackobama.com",
Re:Jshon: A command line program to parse, read and create JSON by keenerd on Friday February 18, 2011 @10:39AM (#2235791)
Please never use grep on json. If you are going to use python as a heavyweight json_reformat, at least have the decency to use all of python's excellent json lib.
Re:Jshon: A command line program to parse, read and create JSON by justincormack on Friday February 18, 2011 @05:57AM (#2235001)
There have been some attempts to make a line oriented XML format so that shell tools work; doing the same thing with JSON is possible too; there are a few ways to do it. Have been meaning to give it a go at some point. In my view it makes more sense than building a new set of tools, especially as the Jshon ones do not really seem to compose properly.

References for XML: http://www.lbreyer.com/unix_xml-1.html http://www.ofb.net/~egnor/xml2/ http://gnosis.cx/publish/programming/xml_matters_17.html

Re:Jshon: A command line program to parse, read and create JSON by reirob on Friday February 18, 2011 @08:46AM (#2235353)
I have been using XML Starlet (http://xmlstar.sourceforge.net/) for many years in combination with Unix Shell. It is a very nice command line tool allowing to extract data from XML documents (using XPath expressions), to edit XML documents and even to convert XML to PYX format. The only problem it has so far is with big XML files as it loads them in the memory. But there is work going on to allow extract data without loading whole XML file in RAM.
Re:Jshon: A command line program to parse, read and create JSON by keenerd on Friday February 18, 2011 @08:35AM (#2235322)
> the Jshon ones do not really seem to compose properly.

Could you elaborate on that a bit?

Re:Jshon: A command line program to parse, read and create JSON by justincormack on Friday February 18, 2011 @10:23AM (#2235711)
Most of the operations do not map any json to json, so you cant pipe them. The whole design principle of the unix command line tools is that they all map lines to lines so anything is composable.

So for json I would want a structure match corresponding to grep, filter and map operations, merges etc each of which maps valid json to valid json.

Re:Jshon: A command line program to parse, read and create JSON by keenerd on Friday February 18, 2011 @10:29AM (#2235742)
Almost half* of the commands map json (stdin) to json (stdout). Extract, modify and insert all take and spit out json. Originally (when it only supported single commands) this is how you had to do everything. But that much piping is a real drag. Anyway, show me a shell json util that is more composable or at least provide a suggestion to improving it :-)

Grep/filter/map are all stuff you do in the shell, after using Jshon to extract the data from the hard-to-parse-safely json.

*The remaining commands are concessions to make line-oriented shell processing easier.

Re:Jshon: A command line program to parse, read and create JSON by VMG on Friday February 18, 2011 @06:26AM (#2235054)
Great tool, was thinking about writing something like that myself. I especially wanted a tool that would print the value of a specific key to STDOUT with a syntax like

    echo "{ 'a': {'b': {'c": 0 } } }" | json-tool "a.b.c"   # echos 0
Maybe your tool does that but I missed it
Re:Jshon: A command line program to parse, read and create JSON by keenerd on Friday February 18, 2011 @08:28AM (#2235296)
Glad you like it. I'll be adding a path-like syntax next. This was just a little one-day hackup to learn getopts.

You can do similar with -e a -e b -e c. Or just make your own wrapper with

jshon $(echo ".a.b.c" | sed 's/./ -e/g')

Re:Jshon: A command line program to parse, read and create JSON by keenerd on Friday February 18, 2011 @11:07AM (#2235937)
jshon $(sed 's/\./ -e/g' <<< ".a.b.c")
Re:Jshon: A command line program to parse, read and create JSON by fictorial on Friday February 18, 2011 @08:34AM (#2235318)
> In Aurphan there is an alarming bit of awk:

Senator, I know awk, and that is not an alarming bit of awk.

Re:Jshon: A command line program to parse, read and create JSON by minimax on Friday February 18, 2011 @09:19AM (#2235458)
It also looks like he swaps out some fairly readable awk for some arguably less readable shell script.
Re:Jshon: A command line program to parse, read and create JSON by SudarshanP on Friday February 18, 2011 @08:12AM (#2235250)
Re:Jshon: A command line program to parse, read and create JSON by akv on Friday February 18, 2011 @09:51AM (#2235575)
It is a bit long, but you can also use Python for parsing the json files from command line:

    echo '{"a": "hello", "b": "world"}' | python -c"import sys,json;j = json.loads(sys.stdin.read());print j['a']"
Re:Jshon: A command line program to parse, read and create JSON by webXL on Friday February 18, 2011 @05:04PM (#2237485)
or in 2.6:

  echo '{"a": "hello", "b": "world"}' | python -mjson.tool
Re:Jshon: A command line program to parse, read and create JSON by timrobinson on Friday February 18, 2011 @04:53AM (#2234903)
Jsawk seems more intuitive, as you get to write JavaScript instead of combining command-line flags: https://github.com/micha/jsawk
Re:Jshon: A command line program to parse, read and create JSON by keenerd on Friday February 18, 2011 @08:29AM (#2235300)
Found that, decided I did not want Spidermonkey on my server running untrusted user generated JS. Also, it seemed ironic to have Spidermonkey be a dependency for my "lightweight" shell scripts.
Re:Jshon: A command line program to parse, read and create JSON by akheron on Saturday February 19, 2011 @04:27AM (#2238785)
This is what I thought too. JSON is lightweight and so should be the tools for using it.
Re:Jshon: A command line program to parse, read and create JSON by bguthrie on Friday February 18, 2011 @10:57AM (#2235887)
"Bugs: Numerous!" Truth in advertising. I love it.