5

Possible Duplicate:
Unix command-line JSON parser?

If I have a config file in JSON and a PHP script to flatten the config file into something like this

database_dbname=sensei
database_password=somerandompassword
memcached_host=localhost
....

can I pipe this to my bash script and make each of the entry above as a variable?

./bin/flatten_config.php config.json | ./bin/my_bash_script.sh

so that in my bash-script I can use values from the config file

mysql -D${database_dbname} -p${database_password} ...
dreftymac
  • 31,404
  • 26
  • 119
  • 182
Jeffrey04
  • 6,138
  • 12
  • 45
  • 68

2 Answers2

5

In bash you could write in your script-file

source <(./bin/flatten_config.php config.json)

bash will take the output of flatten_config.php and parse it like input

don_jones
  • 842
  • 7
  • 13
  • Nice answer, +1. Not sure about brackets though. – Andrejs Cainikovs Oct 17 '11 at 14:37
  • not sure about the bracket, but thanks for the clue :) just found out a bit more on source command here http://www.unix.com/shell-programming-scripting/54347-bash-shell-exec-eval-source-looking-help-understand.html – Jeffrey04 Oct 17 '11 at 14:54
  • 3
    +1. the parentheses are correct: this is [bash process substitution](http://www.gnu.org/software/bash/manual/bashref.html#Process-Substitution) – glenn jackman Oct 17 '11 at 15:02
  • 1
    If you don't have Bash, or want to be portable, try with `eval` and backticks. – tripleee Oct 17 '11 at 15:58
3

Checkout TickTick.

It's a true Bash JSON parser.

#!/bin/bash
. /path/to/ticktick.sh

# File
DATA=`cat data.json`
# cURL
#DATA=`curl http://foobar3000.com/echo/request.json`

tickParse "$DATA"

echo ``pathname``
echo ``headers["user-agent"]``
coolaj86
  • 74,004
  • 20
  • 105
  • 125