Tuesday, January 30, 2024

[SOLVED] Access a JSON key from env variable

Issue

Im using ubuntu to store JSON-encoded text into a environment variable like this:

~ test='{"test": { "mmm":"432"}}'
~ echo $test
{"test": { "mmm":"432"}}

how can i access the key "test" to print {"mmm":"432"}?

something like

~ test='{"test": { "mmm":"432"}}'
~ echo $test.test
{ "mmm":"432"}

Solution

Bash doesn't have json support. You must use a command line jq to process the output.

For example:

echo "$test" | jq .test



Answered By - Carson
Answer Checked By - Mildred Charles (WPSolving Admin)