Gangmax Blog

"jsontools" within "oh-my-zsh"

After “oh-my-zsh“ on my computer is upgraded today, I notice a plugin named “jsontools“. It provides several commands which are handy when dealing with JSON string content:

1
2
3
4
5
6
# Before using the following commands, you need to enable it in "oh-my-zsh",
# which is done by updating the "plugins" line in the "~/.zshrc" file.
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.

Here are some examples how to use the commands:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# "is_json":
~> curl https://coderwall.com/bobwilliams.json | is_json
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 3662 0 3662 0 0 2536 0 --:--:-- 0:00:01 --:--:-- 2536
true

# "pp_json":
~> curl https://coderwall.com/bobwilliams.json | pp_json
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 3662 0 3662 0 0 3165 0 --:--:-- 0:00:01 --:--:-- 3165
{
"id": 26098,
"username": "bobwilliams",
"name": "Bob Williams",
"location": "Charleston, SC",
"karma": 1010,
"accounts": {
"github": "bobwilliams",
"twitter": "_bobwilliams"
},
"about": "hacker, lifter, drummer and happily married, proud father of three\n\n\n[LinkedIn](http://www.linkedin.com/pub/bob-williams/17/a6a/314)\n\n",
"title": "CTO",
"company": "SPARC",
"team": 4521,
"thumbnail": "https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/26098/photo.JPG",
"endorsements": 1010,
"specialities": [],
"badges": [
{
"name": "Platypus",
"description": "Have at least one original repo where scala is the dominant language",
"created_at": "2014-04-22T09:02:31.443Z",
"badge": "https://dj1symwmxvldi.cloudfront.net/assets/badges/platypus-edcb8d16952f9cb27e9a1644d7d38b5606ff8f0c8a55869c4e0d8c42ed4ea637.png"
},
{
"name": "Python",
"description": "Would you expect anything less? Have at least one original repo where Python is the dominant language",
"created_at": "2013-09-30T20:27:44.031Z",
"badge": "https://dj1symwmxvldi.cloudfront.net/assets/badges/python-df746136c133c10a796100239e3563e3419fb4d5a7b8cc9bed181c941f6dbf55.png"
},
{
"name": "Narwhal",
"description": "Have at least one original repo where Clojure is the dominant language",
"created_at": "2013-09-30T20:27:43.755Z",
"badge": "https://dj1symwmxvldi.cloudfront.net/assets/badges/narwhal-35d1b215da3a866b69fa1a7ffdd6d495c2b1f982fa65c7bba167f4fd4289fae7.png"
},
{
"name": "Forked",
"description": "Have a project valued enough to be forked by someone else",
"created_at": "2013-05-02T00:58:40.247Z",
"badge": "https://dj1symwmxvldi.cloudfront.net/assets/badges/forked1-63f23ad054823f6065b4526f8552dbc2c4fbaa75d22db7150d700201218ac426.png"
},
{
"name": "Charity",
"description": "Fork and commit to someone's open source project in need",
"created_at": "2012-07-10T20:51:45.093Z",
"badge": "https://dj1symwmxvldi.cloudfront.net/assets/badges/charity-bab6d575c53894cc9e395db7cdb1f0f91f176fa0cc8122c5f824e672f3d556a4.png"
},
{
"name": "Komodo Dragon",
"description": "Have at least one original repo where Java is the dominant language",
"created_at": "2012-06-28T21:05:58.603Z",
"badge": "https://dj1symwmxvldi.cloudfront.net/assets/badges/komododragon-4a367331904f40950f6ad91fe236c48eec07fe8e0087471dbf7a17c6c6ed7923.png"
},
{
"name": "Walrus",
"description": "The walrus is no stranger to variety. Use at least 4 different languages throughout all your repos",
"created_at": "2012-06-28T21:05:58.577Z",
"badge": "https://dj1symwmxvldi.cloudfront.net/assets/badges/walrus-1f517cbbd8f030099b8386c53d4ee40fece3fa146b6362accb3abddb436482cb.png"
},
{
"name": "Lab",
"description": "Have at least one original repo where C# is the dominant language",
"created_at": "2012-06-28T21:05:58.527Z",
"badge": "https://dj1symwmxvldi.cloudfront.net/assets/badges/labrador-ca2c378266ebe420b137ef13aa296e6f3724bd3379872e99a51bf026c81ed596.png"
},
{
"name": "Kona",
"description": "Have at least one original repo where CoffeeScript is the dominant language",
"created_at": "2012-06-28T21:05:58.427Z",
"badge": "https://dj1symwmxvldi.cloudfront.net/assets/badges/coffee-8704f08f6046466a103a67e28414732e8327b35a4ba700cfb896bb1b67dff554.png"
},
{
"name": "Raven",
"description": "Have at least one original repo where some form of shell script is the dominant language",
"created_at": "2012-06-22T16:53:22.128Z",
"badge": "https://dj1symwmxvldi.cloudfront.net/assets/badges/raven-35923a69492045dffe4dfe0d7ebf31da4e76efabbbdaa090acd59d17debb3087.png"
},
{
"name": "Bear",
"description": "Have at least one original repo where Objective-C is the dominant language",
"created_at": "2012-06-17T10:22:33.320Z",
"badge": "https://dj1symwmxvldi.cloudfront.net/assets/badges/bear-5c19af6347f82c65ad573ca74334eb155a8b3bb617607752895dc6751c6c88fc.png"
}
]
}

# "urlencode_json":
~> curl https://coderwall.com/bobwilliams.json | urlencode_json
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 3662 0 3662 0 0 3252 0 --:--:-- 0:00:01 --:--:-- 3252
%7B%22id%22%3A26098%2C%22username%22%3A%22bobwilliams%22%2C%22name%22%3A%22Bob+Williams%22%2C%22location%22%3A%22Charleston%2C+SC%22%2C%22karma%22%3A1010%2C%22accounts%22%3A%7B%22github%22%3A%22bobwilliams%22%2C%22twitter%22%3A%22_bobwilliams%22%7D%2C%22about%22%3A%22hacker%2C+lifter%2C+drummer+and+happily+married%2C+proud+father+of+three%5Cn%5Cn%5Cn%5BLinkedIn%5D%28http%3A%2F%2Fwww.linkedin.com%2Fpub%2Fbob-williams%2F17%2Fa6a%2F314%29%5Cn%5Cn%22%2C%22title%22%3A%22CTO%22%2C%22company%22%3A%22SPARC%22%2C%22team%22%3A4521%2C%22thumbnail%22%3A%22https%3A%2F%2Fcoderwall-assets-0.s3.amazonaws.com%2Fuploads%2Fuser%2Favatar%2F26098%2Fphoto.JPG%22%2C%22endorsements%22%3A1010%2C%22specialities%22%3A%5B%5D%2C%22badges%22%3A%5B%7B%22name%22%3A%22Platypus%22%2C%22description%22%3A%22Have+at+least+one+original+repo+where+scala+is+the+dominant+language%22%2C%22created_at%22%3A%222014-04-22T09%3A02%3A31.443Z%22%2C%22badge%22%3A%22https%3A%2F%2Fdj1symwmxvldi.cloudfront.net%2Fassets%2Fbadges%2Fplatypus-edcb8d16952f9cb27e9a1644d7d38b5606ff8f0c8a55869c4e0d8c42ed4ea637.png%22%7D%2C%7B%22name%22%3A%22Python%22%2C%22description%22%3A%22Would+you+expect+anything+less%3F+Have+at+least+one+original+repo+where+Python+is+the+dominant+language%22%2C%22created_at%22%3A%222013-09-30T20%3A27%3A44.031Z%22%2C%22badge%22%3A%22https%3A%2F%2Fdj1symwmxvldi.cloudfront.net%2Fassets%2Fbadges%2Fpython-df746136c133c10a796100239e3563e3419fb4d5a7b8cc9bed181c941f6dbf55.png%22%7D%2C%7B%22name%22%3A%22Narwhal%22%2C%22description%22%3A%22Have+at+least+one+original+repo+where+Clojure+is+the+dominant+language%22%2C%22created_at%22%3A%222013-09-30T20%3A27%3A43.755Z%22%2C%22badge%22%3A%22https%3A%2F%2Fdj1symwmxvldi.cloudfront.net%2Fassets%2Fbadges%2Fnarwhal-35d1b215da3a866b69fa1a7ffdd6d495c2b1f982fa65c7bba167f4fd4289fae7.png%22%7D%2C%7B%22name%22%3A%22Forked%22%2C%22description%22%3A%22Have+a+project+valued+enough+to+be+forked+by+someone+else%22%2C%22created_at%22%3A%222013-05-02T00%3A58%3A40.247Z%22%2C%22badge%22%3A%22https%3A%2F%2Fdj1symwmxvldi.cloudfront.net%2Fassets%2Fbadges%2Fforked1-63f23ad054823f6065b4526f8552dbc2c4fbaa75d22db7150d700201218ac426.png%22%7D%2C%7B%22name%22%3A%22Charity%22%2C%22description%22%3A%22Fork+and+commit+to+someone%27s+open+source+project+in+need%22%2C%22created_at%22%3A%222012-07-10T20%3A51%3A45.093Z%22%2C%22badge%22%3A%22https%3A%2F%2Fdj1symwmxvldi.cloudfront.net%2Fassets%2Fbadges%2Fcharity-bab6d575c53894cc9e395db7cdb1f0f91f176fa0cc8122c5f824e672f3d556a4.png%22%7D%2C%7B%22name%22%3A%22Komodo+Dragon%22%2C%22description%22%3A%22Have+at+least+one+original+repo+where+Java+is+the+dominant+language%22%2C%22created_at%22%3A%222012-06-28T21%3A05%3A58.603Z%22%2C%22badge%22%3A%22https%3A%2F%2Fdj1symwmxvldi.cloudfront.net%2Fassets%2Fbadges%2Fkomododragon-4a367331904f40950f6ad91fe236c48eec07fe8e0087471dbf7a17c6c6ed7923.png%22%7D%2C%7B%22name%22%3A%22Walrus%22%2C%22description%22%3A%22The+walrus+is+no+stranger+to+variety.+Use+at+least+4+different+languages+throughout+all+your+repos%22%2C%22created_at%22%3A%222012-06-28T21%3A05%3A58.577Z%22%2C%22badge%22%3A%22https%3A%2F%2Fdj1symwmxvldi.cloudfront.net%2Fassets%2Fbadges%2Fwalrus-1f517cbbd8f030099b8386c53d4ee40fece3fa146b6362accb3abddb436482cb.png%22%7D%2C%7B%22name%22%3A%22Lab%22%2C%22description%22%3A%22Have+at+least+one+original+repo+where+C%23+is+the+dominant+language%22%2C%22created_at%22%3A%222012-06-28T21%3A05%3A58.527Z%22%2C%22badge%22%3A%22https%3A%2F%2Fdj1symwmxvldi.cloudfront.net%2Fassets%2Fbadges%2Flabrador-ca2c378266ebe420b137ef13aa296e6f3724bd3379872e99a51bf026c81ed596.png%22%7D%2C%7B%22name%22%3A%22Kona%22%2C%22description%22%3A%22Have+at+least+one+original+repo+where+CoffeeScript+is+the+dominant+language%22%2C%22created_at%22%3A%222012-06-28T21%3A05%3A58.427Z%22%2C%22badge%22%3A%22https%3A%2F%2Fdj1symwmxvldi.cloudfront.net%2Fassets%2Fbadges%2Fcoffee-8704f08f6046466a103a67e28414732e8327b35a4ba700cfb896bb1b67dff554.png%22%7D%2C%7B%22name%22%3A%22Raven%22%2C%22description%22%3A%22Have+at+least+one+original+repo+where+some+form+of+shell+script+is+the+dominant+language%22%2C%22created_at%22%3A%222012-06-22T16%3A53%3A22.128Z%22%2C%22badge%22%3A%22https%3A%2F%2Fdj1symwmxvldi.cloudfront.net%2Fassets%2Fbadges%2Fraven-35923a69492045dffe4dfe0d7ebf31da4e76efabbbdaa090acd59d17debb3087.png%22%7D%2C%7B%22name%22%3A%22Bear%22%2C%22description%22%3A%22Have+at+least+one+original+repo+where+Objective-C+is+the+dominant+language%22%2C%22created_at%22%3A%222012-06-17T10%3A22%3A33.320Z%22%2C%22badge%22%3A%22https%3A%2F%2Fdj1symwmxvldi.cloudfront.net%2Fassets%2Fbadges%2Fbear-5c19af6347f82c65ad573ca74334eb155a8b3bb617607752895dc6751c6c88fc.png%22%7D%5D%7D

# "urldecode_json":
~> curl https://coderwall.com/bobwilliams.json | urlencode_json | urldecode_json
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 3662 0 3662 0 0 3013 0 --:--:-- 0:00:01 --:--:-- 3013
{"id":26098,"username":"bobwilliams","name":"Bob Williams","location":"Charleston, SC","karma":1010,"accounts":{"github":"bobwilliams","twitter":"_bobwilliams"},"about":"hacker, lifter, drummer and happily married, proud father of three\n\n\n[LinkedIn](http://www.linkedin.com/pub/bob-williams/17/a6a/314)\n\n","title":"CTO","company":"SPARC","team":4521,"thumbnail":"https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/26098/photo.JPG","endorsements":1010,"specialities":[],"badges":[{"name":"Platypus","description":"Have at least one original repo where scala is the dominant language","created_at":"2014-04-22T09:02:31.443Z","badge":"https://dj1symwmxvldi.cloudfront.net/assets/badges/platypus-edcb8d16952f9cb27e9a1644d7d38b5606ff8f0c8a55869c4e0d8c42ed4ea637.png"},{"name":"Python","description":"Would you expect anything less? Have at least one original repo where Python is the dominant language","created_at":"2013-09-30T20:27:44.031Z","badge":"https://dj1symwmxvldi.cloudfront.net/assets/badges/python-df746136c133c10a796100239e3563e3419fb4d5a7b8cc9bed181c941f6dbf55.png"},{"name":"Narwhal","description":"Have at least one original repo where Clojure is the dominant language","created_at":"2013-09-30T20:27:43.755Z","badge":"https://dj1symwmxvldi.cloudfront.net/assets/badges/narwhal-35d1b215da3a866b69fa1a7ffdd6d495c2b1f982fa65c7bba167f4fd4289fae7.png"},{"name":"Forked","description":"Have a project valued enough to be forked by someone else","created_at":"2013-05-02T00:58:40.247Z","badge":"https://dj1symwmxvldi.cloudfront.net/assets/badges/forked1-63f23ad054823f6065b4526f8552dbc2c4fbaa75d22db7150d700201218ac426.png"},{"name":"Charity","description":"Fork and commit to someone's open source project in need","created_at":"2012-07-10T20:51:45.093Z","badge":"https://dj1symwmxvldi.cloudfront.net/assets/badges/charity-bab6d575c53894cc9e395db7cdb1f0f91f176fa0cc8122c5f824e672f3d556a4.png"},{"name":"Komodo Dragon","description":"Have at least one original repo where Java is the dominant language","created_at":"2012-06-28T21:05:58.603Z","badge":"https://dj1symwmxvldi.cloudfront.net/assets/badges/komododragon-4a367331904f40950f6ad91fe236c48eec07fe8e0087471dbf7a17c6c6ed7923.png"},{"name":"Walrus","description":"The walrus is no stranger to variety. Use at least 4 different languages throughout all your repos","created_at":"2012-06-28T21:05:58.577Z","badge":"https://dj1symwmxvldi.cloudfront.net/assets/badges/walrus-1f517cbbd8f030099b8386c53d4ee40fece3fa146b6362accb3abddb436482cb.png"},{"name":"Lab","description":"Have at least one original repo where C# is the dominant language","created_at":"2012-06-28T21:05:58.527Z","badge":"https://dj1symwmxvldi.cloudfront.net/assets/badges/labrador-ca2c378266ebe420b137ef13aa296e6f3724bd3379872e99a51bf026c81ed596.png"},{"name":"Kona","description":"Have at least one original repo where CoffeeScript is the dominant language","created_at":"2012-06-28T21:05:58.427Z","badge":"https://dj1symwmxvldi.cloudfront.net/assets/badges/coffee-8704f08f6046466a103a67e28414732e8327b35a4ba700cfb896bb1b67dff554.png"},{"name":"Raven","description":"Have at least one original repo where some form of shell script is the dominant language","created_at":"2012-06-22T16:53:22.128Z","badge":"https://dj1symwmxvldi.cloudfront.net/assets/badges/raven-35923a69492045dffe4dfe0d7ebf31da4e76efabbbdaa090acd59d17debb3087.png"},{"name":"Bear","description":"Have at least one original repo where Objective-C is the dominant language","created_at":"2012-06-17T10:22:33.320Z","badge":"https://dj1symwmxvldi.cloudfront.net/assets/badges/bear-5c19af6347f82c65ad573ca74334eb155a8b3bb617607752895dc6751c6c88fc.png"}]}

One more interesting thing is the implementation of the commands:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
~> which is_json
is_json () {
python -c '
import sys; del sys.path[0];
import json
try:
json.loads(sys.stdin.read())
print("true"); sys.exit(0)
except ValueError:
print("false"); sys.exit(1)
'
}

~> which pp_json
pp_json () {
python -c 'import sys; del sys.path[0]; import runpy; runpy._run_module_as_main("json.tool")'
}

~> which urlencode_json
urlencode_json () {
python -c '
import sys; del sys.path[0];
from urllib.parse import quote_plus
print(quote_plus(sys.stdin.read()))
'
}

~> which urldecode_json
urldecode_json () {
python -c '
import sys; del sys.path[0];
from urllib.parse import unquote_plus
print(unquote_plus(sys.stdin.read()))
'
}

Comments