Last active
August 29, 2015 14:14
-
-
Save narate/5712055078395429cd6f to your computer and use it in GitHub Desktop.
Command line JSON parser.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env luajit | |
-- author : Narate Ketram | |
-- github.com/narate | |
-- $ echo '{"name":"narate"}' | parse_json name | |
-- "narate" | |
local cjson = require 'cjson.safe' | |
local data = io.read('*all') | |
local json = cjson.decode(data) | |
if not json then | |
print('Invalid json') | |
return | |
end | |
local key = tonumber(arg[1]) or arg[1] | |
if not key then | |
print(data) | |
return | |
end | |
print(cjson.encode(json[key]) or 'not found') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ผมลง luajit และใช้ luarocks ลง lua-cjson แต่ว่ามันหา cjson ไม่เจอนะครับ ทำยังไงดี