I am a software developer who specializes in web applications. My languages of choice are PHP and Javascript, but I dabble in a little bit of everything. I am the author of the Neo4jPHP binding for the Neo4j graph database. Every part of the software development process fascinates me. Josh is a DZone MVB and is not an employee of DZone and has posted 14 posts at DZone. You can read more from them at their website. View Full User Profile

Decode JSON in Bash with PHP

04.26.2012
| 4129 views |
  • submit to reddit
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
Published at DZone with permission of Josh Adell, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: