Bash - Read key value from JSON file

#!/bin/bash : ' Read JSON Value from file. Data must be on single line. /config.json {"foo":"bar"} ' file='config.json' key='foo' value=$(cat $file | sed 's/{.*\'$key'":"*\([0-9a-zA-Z]*\)"*,*.*}/\1/') echo "The value is " $value # ALTERNATE FUNCTION ############################################## function get_value { key=$1 value=$(cat $file | sed 's/{.*\'$key'":"*\([0-9a-zA-Z|.|_|-|\/]*\)"*,*.*}/\1/') echo $value } foo=$(get_value 'foo') echo $foo
The big caveat to all this; it will only work with JSON on 1 line. As soon as your break up the lines this fails to capture the data. So make your data really ugly -- NOT Pretty.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.