Skip to content

Instantly share code, notes, and snippets.

@narate
Last active August 29, 2015 14:14
Show Gist options
  • Save narate/5712055078395429cd6f to your computer and use it in GitHub Desktop.
Save narate/5712055078395429cd6f to your computer and use it in GitHub Desktop.
Command line JSON parser.
#!/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')
@iporsut
Copy link

iporsut commented Mar 11, 2015

ผมลง luajit และใช้ luarocks ลง lua-cjson แต่ว่ามันหา cjson ไม่เจอนะครับ ทำยังไงดี

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment