Simple CLI json query tool
If you using CLI REST clients like Resty (https://github.com/micha/resty) sometimes you want to make queries to received json.
This simple ruby script will help you:
#!/usr/bin/env ruby
# Very simple and dirty json CLI query tool
require 'rubygems'
require 'json'
def numeric?(s)
Float(s) rescue false
end
this = JSON[STDIN.read]
js_path = ARGV[0].split('.')
unless ARGV[0] == "-e"
query_string = "#{js_path[0]}"
query_string = query_string + js_path[1..-1].reduce("") do |s, p|
if numeric? p
"#{s}[#{p}]"
else
"#{s}['#{p}']"
end
end
else
query_string = ARGV[1]
end
result = eval(query_string)
begin
puts JSON.pretty_generate(result)
rescue Exception => e
puts result
end
Usage:
~ :λ GET /semka
{
"id" : "137",
"type" : "user",
"name" : "Սեմյոն Նովիկով",
"login" : "semka",
"firstName" : "Սեմյոն",
"lastName" : "Նովիկով",
"picture" : "pic_d71fb6d4-e7c6-4beb-985b-c2f71d981987",
"scores" : 27,
"maxScores" : 49,
"title" : "pedestrian",
"followed" : false,
"privateAccount" : true,
"qTrainings" : 36,
"qPhotos" : 110,
"sex" : "M",
"mealsPerDay" : 5
}
~ :λ GET /semka | json -e 'this["login"]'
semka
~ :λ GET /semka | json -e 'this["name"]'
Սեմյոն Նովիկով
~ :λ
Or even that way:
~ :λ GET /denbl
{
"id" : "120",
"type" : "user",
"name" : "Denis Blinov",
"login" : "denbl",
"firstName" : "Denis",
"lastName" : "Blinov",
"picture" : "pic_f105874c-0ad9-466c-8291-f91cba414aac",
"scores" : 86,
"maxScores" : 137,
"dateOfBirth" : "1987-11-28",
"title" : "cycler",
"facebookName" : "denbl",
"facebookUrl" : "http://www.facebook.com/denbl",
"followed" : false,
"privateAccount" : false,
"qFollowed" : 198,
"qFollowers" : 581,
"qTrainings" : 185,
"qPhotos" : 560,
"sex" : "M",
"twitterName" : "denbl",
"twitterUrl" : "http://twitter.com/denbl",
"qBadges" : 18,
"board" : [ {
"id" : "102953",
"name" : "Ilya Pavlenkov",
"login" : "sirjartur",
"picture" : "pic_cf0e66c4-e01f-4595-bc88-a8a314cd2ae7",
"scores" : 94,
"position" : 6,
"title" : "cycler",
"followed" : false,
"qTrainings" : 15,
"qPhotos" : 1,
"sex" : "M",
"mealsPerDay" : 5
}, {
"id" : "3819",
"name" : "Кирилл Чернаков",
"login" : "kirys",
"picture" : "pic_01740f92-5d1f-48ee-a628-fb2fa188df35",
"scores" : 90,
"position" : 7,
"comment" : "",
"title" : "cycler",
"followed" : false,
"qTrainings" : 79,
"qPhotos" : 78,
"sex" : "M",
"mealsPerDay" : 5
}, {
"id" : "120",
"name" : "Denis Blinov",
"login" : "denbl",
"picture" : "pic_f105874c-0ad9-466c-8291-f91cba414aac",
"scores" : 86,
"position" : 8,
"title" : "cycler",
"followed" : false,
"qTrainings" : 185,
"qPhotos" : 560,
"sex" : "M",
"mealsPerDay" : 5
}, {
"id" : "7887",
"name" : "Alexe",
"login" : "alexe",
"picture" : "pic_45c6a357-9905-47fa-9504-2363975d1a1c",
"scores" : 75,
"position" : 9,
"comment" : "",
"title" : "cycler",
"followed" : false,
"qTrainings" : 66,
"qPhotos" : 4,
"sex" : "M",
"mealsPerDay" : 5
}, {
"id" : "159575",
"name" : "Igor Stepahin",
"login" : "stepahin",
"picture" : "pic_925b58b7-2c7e-4f8a-8edf-853af4eca331",
"scores" : 70,
"position" : 10,
"title" : "cycler",
"followed" : false,
"qTrainings" : 41,
"qPhotos" : 29,
"sex" : "M",
"mealsPerDay" : 5
} ],
"mealsPerDay" : 5
}
~ :λ GET /denbl | json -e 'this["board"]'
[
{
"mealsPerDay": 5,
"followed": false,
"title": "cycler",
"position": 6,
"qPhotos": 1,
"qTrainings": 15,
"sex": "M",
"picture": "pic_cf0e66c4-e01f-4595-bc88-a8a314cd2ae7",
"name": "Ilya Pavlenkov",
"scores": 94,
"login": "sirjartur",
"id": "102953"
},
{
"mealsPerDay": 5,
"followed": false,
"title": "cycler",
"position": 7,
"qPhotos": 78,
"qTrainings": 79,
"sex": "M",
"picture": "pic_01740f92-5d1f-48ee-a628-fb2fa188df35",
"name": "Кирилл Чернаков",
"scores": 90,
"login": "kirys",
"comment": "",
"id": "3819"
},
{
"mealsPerDay": 5,
"followed": false,
"title": "cycler",
"position": 8,
"qPhotos": 560,
"qTrainings": 185,
"sex": "M",
"picture": "pic_f105874c-0ad9-466c-8291-f91cba414aac",
"name": "Denis Blinov",
"scores": 86,
"login": "denbl",
"id": "120"
},
{
"mealsPerDay": 5,
"followed": false,
"title": "cycler",
"position": 9,
"qPhotos": 4,
"qTrainings": 66,
"sex": "M",
"picture": "pic_45c6a357-9905-47fa-9504-2363975d1a1c",
"name": "Alexe",
"scores": 75,
"login": "alexe",
"comment": "",
"id": "7887"
},
{
"mealsPerDay": 5,
"followed": false,
"title": "cycler",
"position": 10,
"qPhotos": 29,
"qTrainings": 41,
"sex": "M",
"picture": "pic_925b58b7-2c7e-4f8a-8edf-853af4eca331",
"name": "Igor Stepahin",
"scores": 70,
"login": "stepahin",
"id": "159575"
}
]
~ :λ GET /denbl | json -e 'this["board"][0]'
{
"sex": "M",
"followed": false,
"picture": "pic_cf0e66c4-e01f-4595-bc88-a8a314cd2ae7",
"qTrainings": 15,
"scores": 94,
"qPhotos": 1,
"title": "cycler",
"mealsPerDay": 5,
"name": "Ilya Pavlenkov",
"login": "sirjartur",
"position": 6,
"id": "102953"
}
Written by Sёmka Novikov
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#