読者です 読者をやめる 読者になる 読者になる

詩と創作・思索のひろば

ドキドキギュンギュンダイアリーです!!!

Parsing JSON input and inspecting in a REPL

Perl JSON

When you are to inspect a JSON, if you are already familier with its format, using jq may be a solution. But if you don't know its structure well? A REPL-way would be handy.

Using Reply, in .replyrc:

; .replyrc
script_line5 = sub j { our $j ||= do { require JSON; local $/; JSON::decode_json(scalar <STDIN>) } }

[ReadLine]

Enabling ReadLine plugin is required.

With this configuration, you can feed JSON input to reply cli. j() returns that JSON parsed.

% curl -s https://api.github.com/users/motemen | reply
0> j->{blog}
$res[0] = 'http://motemen.github.io/'
1> keys j
$res[1] = [
  'gravatar_id',
  'subscriptions_url',
  'organizations_url',
  …
]