From 3bae9dd2bfc59e03a6aeded95ca45d2810058e09 Mon Sep 17 00:00:00 2001 From: Bob Williams Date: Sun, 23 Mar 2014 15:12:18 -0400 Subject: [PATCH 1/8] adding README.md for jsontools --- plugins/jsontools/README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 plugins/jsontools/README.md diff --git a/plugins/jsontools/README.md b/plugins/jsontools/README.md new file mode 100644 index 000000000..ed7a19d29 --- /dev/null +++ b/plugins/jsontools/README.md @@ -0,0 +1,17 @@ +# jsontools + +# Plugin name + +A handy command line tool for dealing with json data. + +## Usage + +```sh +# curl json data and pretty print the results +curl https://coderwall.com/bobwilliams.json | pp_json + +# pretty print the contents of an existing json file +less data.json | pp_json + +# json data directly from the command line +echo '{"b":2, "a":1}' | pp_json \ No newline at end of file From 1fc40c60b506bc71f20efcafc7e7350ece3a1596 Mon Sep 17 00:00:00 2001 From: Bob Williams Date: Sun, 23 Mar 2014 15:13:41 -0400 Subject: [PATCH 2/8] updating README.md --- plugins/jsontools/README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/plugins/jsontools/README.md b/plugins/jsontools/README.md index ed7a19d29..60f99c5be 100644 --- a/plugins/jsontools/README.md +++ b/plugins/jsontools/README.md @@ -1,7 +1,5 @@ # jsontools -# Plugin name - A handy command line tool for dealing with json data. ## Usage From 8db3e6329d25ae3e7d6c4996b5a0c3a5f31d645e Mon Sep 17 00:00:00 2001 From: Bob Williams Date: Mon, 24 Mar 2014 13:36:26 -0400 Subject: [PATCH 3/8] bug fix for node pp_json version --- plugins/jsontools/jsontools.plugin.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/jsontools/jsontools.plugin.zsh b/plugins/jsontools/jsontools.plugin.zsh index 7b6f8c585..ee092ff09 100644 --- a/plugins/jsontools/jsontools.plugin.zsh +++ b/plugins/jsontools/jsontools.plugin.zsh @@ -6,7 +6,7 @@ if [[ $(whence $JSONTOOLS_METHOD) = "" ]]; then fi if [[ $(whence node) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xnode" ) ]]; then - alias pp_json='node -e "console.log(JSON.stringify(process.argv[1]), null, 4)"' + alias pp_json='node -e "console.log(JSON.stringify(JSON.parse(process.argv[1]), null, 4));"' elif [[ $(whence python) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xpython" ) ]]; then alias pp_json='python -mjson.tool' elif [[ $(whence ruby) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xruby" ) ]]; then From f8890f564648d647903f5b10358a4f25f312d54a Mon Sep 17 00:00:00 2001 From: Bob Williams Date: Mon, 24 Mar 2014 14:09:57 -0400 Subject: [PATCH 4/8] updated readme.md and using tab char for formatting with node --- plugins/jsontools/README.md | 11 +++++++++-- plugins/jsontools/jsontools.plugin.zsh | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/plugins/jsontools/README.md b/plugins/jsontools/README.md index 60f99c5be..9f07194c9 100644 --- a/plugins/jsontools/README.md +++ b/plugins/jsontools/README.md @@ -1,9 +1,15 @@ # jsontools -A handy command line tool for dealing with json data. +Handy command line tools for dealing with json data. + +## Tools + +- **pp_json** - pretty prints json ## Usage +##### pp_json - examples + ```sh # curl json data and pretty print the results curl https://coderwall.com/bobwilliams.json | pp_json @@ -12,4 +18,5 @@ curl https://coderwall.com/bobwilliams.json | pp_json less data.json | pp_json # json data directly from the command line -echo '{"b":2, "a":1}' | pp_json \ No newline at end of file +echo '{"b":2, "a":1}' | pp_json +``` \ No newline at end of file diff --git a/plugins/jsontools/jsontools.plugin.zsh b/plugins/jsontools/jsontools.plugin.zsh index ee092ff09..34ff9cc75 100644 --- a/plugins/jsontools/jsontools.plugin.zsh +++ b/plugins/jsontools/jsontools.plugin.zsh @@ -6,7 +6,7 @@ if [[ $(whence $JSONTOOLS_METHOD) = "" ]]; then fi if [[ $(whence node) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xnode" ) ]]; then - alias pp_json='node -e "console.log(JSON.stringify(JSON.parse(process.argv[1]), null, 4));"' + alias pp_json='node -e "console.log(JSON.stringify(JSON.parse(process.argv[1]), null, '\t'));"' elif [[ $(whence python) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xpython" ) ]]; then alias pp_json='python -mjson.tool' elif [[ $(whence ruby) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xruby" ) ]]; then From 64c3ce35e1493728c65191d9b16183841ad73ca7 Mon Sep 17 00:00:00 2001 From: Bob Williams Date: Mon, 24 Mar 2014 14:24:00 -0400 Subject: [PATCH 5/8] adding xargs -0 to node aliases --- plugins/jsontools/jsontools.plugin.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/jsontools/jsontools.plugin.zsh b/plugins/jsontools/jsontools.plugin.zsh index 34ff9cc75..729763c33 100644 --- a/plugins/jsontools/jsontools.plugin.zsh +++ b/plugins/jsontools/jsontools.plugin.zsh @@ -6,7 +6,7 @@ if [[ $(whence $JSONTOOLS_METHOD) = "" ]]; then fi if [[ $(whence node) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xnode" ) ]]; then - alias pp_json='node -e "console.log(JSON.stringify(JSON.parse(process.argv[1]), null, '\t'));"' + alias pp_json='xargs -0 node -e "console.log(JSON.stringify(JSON.parse(process.argv[1]), null, 4));"' elif [[ $(whence python) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xpython" ) ]]; then alias pp_json='python -mjson.tool' elif [[ $(whence ruby) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xruby" ) ]]; then From 982b534ce8cdf439c96bc48ff58f890e9e350d30 Mon Sep 17 00:00:00 2001 From: Bob Williams Date: Tue, 25 Mar 2014 16:20:25 -0400 Subject: [PATCH 6/8] adding the is_json tool and associated readme.md details --- plugins/jsontools/README.md | 17 +++++++++++++++-- plugins/jsontools/jsontools.plugin.zsh | 15 +++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/plugins/jsontools/README.md b/plugins/jsontools/README.md index 9f07194c9..309ce9b5f 100644 --- a/plugins/jsontools/README.md +++ b/plugins/jsontools/README.md @@ -5,10 +5,11 @@ Handy command line tools for dealing with json data. ## Tools - **pp_json** - pretty prints json +- **is_json** - returns true if valid json; false otherwise -## Usage +## Examples -##### pp_json - examples +##### pp_json ```sh # curl json data and pretty print the results @@ -19,4 +20,16 @@ less data.json | pp_json # json data directly from the command line echo '{"b":2, "a":1}' | pp_json +``` + +##### is_json +```sh +# curl json data and pretty print the results +curl https://coderwall.com/bobwilliams.json | is_json + +# pretty print the contents of an existing json file +less data.json | is_json + +# json data directly from the command line +echo '{"b":2, "a":1}' | is_json ``` \ No newline at end of file diff --git a/plugins/jsontools/jsontools.plugin.zsh b/plugins/jsontools/jsontools.plugin.zsh index 729763c33..b8b558807 100644 --- a/plugins/jsontools/jsontools.plugin.zsh +++ b/plugins/jsontools/jsontools.plugin.zsh @@ -2,15 +2,26 @@ # Adds command line aliases useful for dealing with JSON if [[ $(whence $JSONTOOLS_METHOD) = "" ]]; then - JSONTOOLS_METHOD="" + JSONTOOLS_METHOD="" fi if [[ $(whence node) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xnode" ) ]]; then alias pp_json='xargs -0 node -e "console.log(JSON.stringify(JSON.parse(process.argv[1]), null, 4));"' + 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); }"' elif [[ $(whence python) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xpython" ) ]]; then alias pp_json='python -mjson.tool' + alias is_json='python -c " +import json, sys; +try: + json.loads(sys.stdin.read()) +except ValueError, e: + print False +else: + print True +sys.exit(0)"' elif [[ $(whence ruby) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xruby" ) ]]; then alias pp_json='ruby -e "require \"json\"; require \"yaml\"; puts JSON.parse(STDIN.read).to_yaml"' + alias is_json='ruby -e "require \"json\"; begin; JSON.parse(STDIN.read); puts true; rescue Exception => e; puts false; end"' fi -unset JSONTOOLS_METHOD \ No newline at end of file +unset JSONTOOLS_METHOD From 0605d53fa7f04c864e7bc5f9f68bd38f24fbbfd5 Mon Sep 17 00:00:00 2001 From: Bob Williams Date: Wed, 26 Mar 2014 18:47:57 -0400 Subject: [PATCH 7/8] adding urlencode_json and associated README.md details --- plugins/jsontools/README.md | 20 ++++++++++---------- plugins/jsontools/jsontools.plugin.zsh | 6 ++++++ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/plugins/jsontools/README.md b/plugins/jsontools/README.md index 309ce9b5f..975d2bb79 100644 --- a/plugins/jsontools/README.md +++ b/plugins/jsontools/README.md @@ -6,7 +6,13 @@ Handy command line tools for dealing with json data. - **pp_json** - pretty prints json - **is_json** - returns true if valid json; false otherwise +- **urlencode_json** - returns a url encoded string for the given json +## Usage +Usage is simple...just take your json data and pipe it into the appropriate jsontool. +```sh + | +``` ## Examples ##### pp_json @@ -14,22 +20,16 @@ Handy command line tools for dealing with json data. ```sh # curl json data and pretty print the results curl https://coderwall.com/bobwilliams.json | pp_json - -# pretty print the contents of an existing json file -less data.json | pp_json - -# json data directly from the command line -echo '{"b":2, "a":1}' | pp_json ``` ##### is_json ```sh -# curl json data and pretty print the results -curl https://coderwall.com/bobwilliams.json | is_json - # pretty print the contents of an existing json file less data.json | is_json +``` +##### urlencode_json +```sh # json data directly from the command line -echo '{"b":2, "a":1}' | is_json +echo '{"b":2, "a":1}' | urlencode_json ``` \ No newline at end of file diff --git a/plugins/jsontools/jsontools.plugin.zsh b/plugins/jsontools/jsontools.plugin.zsh index b8b558807..8877ee62f 100644 --- a/plugins/jsontools/jsontools.plugin.zsh +++ b/plugins/jsontools/jsontools.plugin.zsh @@ -8,6 +8,7 @@ fi if [[ $(whence node) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xnode" ) ]]; then alias pp_json='xargs -0 node -e "console.log(JSON.stringify(JSON.parse(process.argv[1]), null, 4));"' 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); }"' + alias urlencode_json='xargs -0 node -e "console.log(encodeURIComponent(JSON.stringify(process.argv[1])))"' elif [[ $(whence python) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xpython" ) ]]; then alias pp_json='python -mjson.tool' alias is_json='python -c " @@ -18,10 +19,15 @@ except ValueError, e: print False else: print True +sys.exit(0)"' + alias urlencode_json='python -c " +import urllib, json, sys; +print urllib.quote_plus(json.dumps(sys.stdin.read())) sys.exit(0)"' elif [[ $(whence ruby) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xruby" ) ]]; then alias pp_json='ruby -e "require \"json\"; require \"yaml\"; puts JSON.parse(STDIN.read).to_yaml"' alias is_json='ruby -e "require \"json\"; begin; JSON.parse(STDIN.read); puts true; rescue Exception => e; puts false; end"' + alias urlencode_json='ruby -e "require \"uri\"; puts URI.escape(STDIN.read)"' fi unset JSONTOOLS_METHOD From 400643ff5b20521aebe16ca36fde34bde212db89 Mon Sep 17 00:00:00 2001 From: Bob Williams Date: Thu, 27 Mar 2014 10:34:54 -0400 Subject: [PATCH 8/8] adding urldecode_json to compliment urlencode_json and updating readme.md; slight tweak to urlencode_json from previous commit --- plugins/jsontools/README.md | 7 +++++++ plugins/jsontools/jsontools.plugin.zsh | 12 +++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/plugins/jsontools/README.md b/plugins/jsontools/README.md index 975d2bb79..4faf58b98 100644 --- a/plugins/jsontools/README.md +++ b/plugins/jsontools/README.md @@ -7,6 +7,7 @@ Handy command line tools for dealing with json data. - **pp_json** - pretty prints json - **is_json** - returns true if valid json; false otherwise - **urlencode_json** - returns a url encoded string for the given json +- **urldecode_json** - returns decoded json for the given url encoded string ## Usage Usage is simple...just take your json data and pipe it into the appropriate jsontool. @@ -32,4 +33,10 @@ less data.json | is_json ```sh # json data directly from the command line echo '{"b":2, "a":1}' | urlencode_json +``` + +##### urldecode_json +```sh +# url encoded string to decode +echo '%7B%22b%22:2,%20%22a%22:1%7D%0A' | urldecode_json ``` \ No newline at end of file diff --git a/plugins/jsontools/jsontools.plugin.zsh b/plugins/jsontools/jsontools.plugin.zsh index 8877ee62f..20d5eb1c9 100644 --- a/plugins/jsontools/jsontools.plugin.zsh +++ b/plugins/jsontools/jsontools.plugin.zsh @@ -8,7 +8,8 @@ fi if [[ $(whence node) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xnode" ) ]]; then alias pp_json='xargs -0 node -e "console.log(JSON.stringify(JSON.parse(process.argv[1]), null, 4));"' 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); }"' - alias urlencode_json='xargs -0 node -e "console.log(encodeURIComponent(JSON.stringify(process.argv[1])))"' + alias urlencode_json='xargs -0 node -e "console.log(encodeURIComponent(process.argv[1]))"' + alias urldecode_json='xargs -0 node -e "console.log(decodeURIComponent(process.argv[1]))"' elif [[ $(whence python) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xpython" ) ]]; then alias pp_json='python -mjson.tool' alias is_json='python -c " @@ -22,12 +23,17 @@ else: sys.exit(0)"' alias urlencode_json='python -c " import urllib, json, sys; -print urllib.quote_plus(json.dumps(sys.stdin.read())) +print urllib.quote_plus(sys.stdin.read()) +sys.exit(0)"' + alias urldecode_json='python -c " +import urllib, json, sys; +print urllib.unquote_plus(sys.stdin.read()) sys.exit(0)"' elif [[ $(whence ruby) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xruby" ) ]]; then alias pp_json='ruby -e "require \"json\"; require \"yaml\"; puts JSON.parse(STDIN.read).to_yaml"' alias is_json='ruby -e "require \"json\"; begin; JSON.parse(STDIN.read); puts true; rescue Exception => e; puts false; end"' alias urlencode_json='ruby -e "require \"uri\"; puts URI.escape(STDIN.read)"' + alias urldecode_json='ruby -e "require \"uri\"; puts URI.unescape(STDIN.read)"' fi -unset JSONTOOLS_METHOD +unset JSONTOOLS_METHOD \ No newline at end of file