Diff
1diff --git a/plugins/jsontools/README.md b/plugins/jsontools/README.md
2index 9f07194c90f8f8495a0d2c379972d87f61fe3611..309ce9b5f30c9a743cb18391a9e40be1604a8497 100644
3--- a/plugins/jsontools/README.md
4+++ b/plugins/jsontools/README.md
5@@ -5,10 +5,11 @@ Handy command line tools for dealing with json data.
6 ## Tools
7
8 - **pp_json** - pretty prints json
9+- **is_json** - returns true if valid json; false otherwise
10
11-## Usage
12+## Examples
13
14-##### pp_json - examples
15+##### pp_json
16
17 ```sh
18 # curl json data and pretty print the results
19@@ -19,4 +20,16 @@ less data.json | pp_json
20
21 # json data directly from the command line
22 echo '{"b":2, "a":1}' | pp_json
23+```
24+
25+##### is_json
26+```sh
27+# curl json data and pretty print the results
28+curl https://coderwall.com/bobwilliams.json | is_json
29+
30+# pretty print the contents of an existing json file
31+less data.json | is_json
32+
33+# json data directly from the command line
34+echo '{"b":2, "a":1}' | is_json
35 ```
36diff --git a/plugins/jsontools/jsontools.plugin.zsh b/plugins/jsontools/jsontools.plugin.zsh
37index 729763c3393d05a0eb84738fc16860c86298b4c0..b8b5588075de9340a76dfe22db85aa9c76bbf40f 100644
38--- a/plugins/jsontools/jsontools.plugin.zsh
39+++ b/plugins/jsontools/jsontools.plugin.zsh
40@@ -2,15 +2,26 @@
41 # Adds command line aliases useful for dealing with JSON
42
43 if [[ $(whence $JSONTOOLS_METHOD) = "" ]]; then
44- JSONTOOLS_METHOD=""
45+ JSONTOOLS_METHOD=""
46 fi
47
48 if [[ $(whence node) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xnode" ) ]]; then
49 alias pp_json='xargs -0 node -e "console.log(JSON.stringify(JSON.parse(process.argv[1]), null, 4));"'
50+ alias is_json='xargs -0 node -e "try {json = JSON.parse(process.argv[1]);} catch (e) { console.log(false); json = null; } if(json) { console.log(true); }"'
51 elif [[ $(whence python) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xpython" ) ]]; then
52 alias pp_json='python -mjson.tool'
53+ alias is_json='python -c "
54+import json, sys;
55+try:
56+ json.loads(sys.stdin.read())
57+except ValueError, e:
58+ print False
59+else:
60+ print True
61+sys.exit(0)"'
62 elif [[ $(whence ruby) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xruby" ) ]]; then
63 alias pp_json='ruby -e "require \"json\"; require \"yaml\"; puts JSON.parse(STDIN.read).to_yaml"'
64+ alias is_json='ruby -e "require \"json\"; begin; JSON.parse(STDIN.read); puts true; rescue Exception => e; puts false; end"'
65 fi
66
67-unset JSONTOOLS_METHOD
68+unset JSONTOOLS_METHOD