2011-12-17

Decode JSON in Bash with PHP

I recently found myself needing to make cUrl calls from the command-line to an endpoint which returned JSON responses. Rather than parsing through the JSON as a string, or downloading some third party tool to format it for me, I created this handy Bash alias that decodes JSON from the command-line using PHP.

Put the following in your ~/.bashrc file:
alias json-decode="php -r 'print_r(json_decode(file_get_contents(\"php://stdin\")));'"

Usage from any process:
echo '{"foo":"bar","baz":"qux"}' | json-decode

Or from a cUrl call:
curl -s http://example.com/json/endpoint | json-decode

1 comment:

  1. wonderful!!!!!!!!!!!!!!!

    wonderful!!!!!!!!!!!!!!!

    ReplyDelete