dreddy
dreddy

Reputation: 463

json string in bash variable

I am trying to use a bash variable to store json.

  testConfig=",{
    \"Classification\": \"mapred-site\",
    \"Properties\": {
        \"mapreduce.map.java.opts\": \"-Xmx2270m\",
        \"mapreduce.map.memory.mb\": \"9712\"
      }
    }"

echo $testConfig Output: ,{

If I give it in a single line it works. But i would like to store values in my variable in a clean format.

I tried using cat >>ECHO That didn't work either

Any help is appreciated as to how I can store this in order to get the output in an expected format. Thanks.

Upvotes: 2

Views: 2367

Answers (2)

hek2mgl
hek2mgl

Reputation: 157947

You may use a here doc as described here:

read -r -d '' testConfig <<'EOF'
{
    "Classification": "mapred-site",
    "Properties": {
        "mapreduce.map.java.opts": "-Xmx2270m",
        "mapreduce.map.memory.mb": "9712"
      }
}
EOF

# Just to demonstrate that it works ...
echo "$testConfig" | jq .

Doing so you can avoid quoting.

Upvotes: 2

Chopi
Chopi

Reputation: 1143

You can use read -d

read -d '' testConfig <<EOF
,{ /
        "Classification": "mapred-site", 
        "Properties": {/
            "mapreduce.map.java.opts": "-Xmx2270m", 
            "mapreduce.map.memory.mb": "9712"
          }
        }
EOF

echo $testConfig;

Upvotes: 0

Related Questions