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:
Usage from any process:
Or from a cUrl call:
Published at DZone with permission of Josh Adell, author and DZone MVB. (source)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-decodeOr from a cUrl call:
curl -s http://example.com/json/endpoint | json-decode
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)





