Convert Bash String to JavaString

KNDheeraj :

I want to convert a String present in the bash variable to Java Supported String style.

For Example: data="{"5": ">10%", "4": "<5%", "3": ">28 months", "2": "20%", "1": 100}"

as echo $data

Giving me this result:

    "{\n" + 
    "    \"1\": 21,\n" + 
    "    \"2\": \"40%\",\n" + 
    "    \"3\": \"<24 months\",\n" + 
    "    \"4\": \"<5%\",\n" + 
    "    \"5\": \">10%\"\n" + 
    "}"

But also I need to transfer this value into a file using echo "String data = $data;" >> file.txt where data is value after processing where it is throwing weird result as

String data = "{
" + 
"   \"5\": \">10%\",
" + 
"   \"4\": \"<5%\",
" + 
"   \"3\": \">28 months\",
" + 
"   \"2\": \"20%\",
" + 
"   \"1\": 100
" + 
"}";

But expected was:

String data = "{\n" + 
        "   \"1\": 50,\n" + 
        "   \"2\": \"40%\",\n" + 
        "   \"3\": \">28 months\",\n" + 
        "   \"4\": \"<5%\",\n" + 
        "   \"5\": \">10%\"\n" + 
        "}";
vfalcao :

Using a greatly adhoc approach: (invoking perl twice)

data='{"5": ">10%", "4": "<5%", "3": ">28 months", "2": "20%", "1": 100}'
echo $data |\
perl -pe 's/{/{\n/; s/}/\n}/; s/, /,\n/g; s/^"/   "/gm' |\
perl -0pe 's/"/\\"/g; s/\n/\\n" + \n/g; s/^/"/gm; s/\}\\n.*$/}"/'

The result is:

 "{\n" +
 "   \"5\": \">10%\",\n" +
 "   \"4\": \"<5%\",\n" +
 "   \"3\": \">28 months\",\n" +
 "   \"2\": \"20%\",\n" +
 "   \"1\": 100\n" +
 "}"

Further testing:

test='{"first" : "1st", "second": "2nd", "third" : "3rd" }'

echo $test |\
 perl -pe 's/{/{\n/; s/}/\n}/; s/, /,\n/g; s/^"/   "/gm' |\
 perl -0pe 's/"/\\"/g; s/\n/\\n" + \n/g; s/^/"/gm; s/\}\\n.*$/}"/'

returns

"{\n" +
"   \"first\" : \"1st\",\n" +
"   \"second\": \"2nd\",\n" +
"   \"third\" : \"3rd\" \n" +
"}" 

As for outputting this new string, try:

data='{"5": ">10%", "4": "<5%", "3": ">28 months", "2": "20%", "1": 100}'
newdata=$(echo $data |\
perl -pe 's/{/{\n/; s/}/\n}/; s/, /,\n/g; s/^"/   "/gm' |\
perl -0pe 's/"/\\"/g; s/\n/\\n" + \n/g; s/^/"/gm; s/\}\\n.*$/}"/')

echo "String data = $newdata" >> /tmp/file.txt

As for the update (*using sh instead of bash, and getting 2 \ts *), try the following (it's getting uglier and uglier...') :

data='{"5": ">10%", "4": "<5%", "3": ">28 months", "2": "20%", "1": 100}'
newdata=$(echo $data |\
perl -pe 's/{/{\n/; s/}/\n}/; s/, /,\n/g; ' |\
perl -0pe 's/"/\\"/g; s/\n/\\n" + \n/g; s/^/"/gm; s/^"/\t\t"/gm; s/^\t\t"/"/; s/\}\\n.*$/}"/')

/bin/echo "String data = $newdata" >> /tmp/file.txt

As for getting the arrays in the same line, as requested by @KNDeeraj, we can use:

data='{"array": ["v1", "v2"], "5": ">10%", "4": "<5%", "3": ">28 months", "2": "20%", "1": 100}'
newdata=$(echo $data |\
perl -pe 's/{/{\n/; s/}/\n}/; s/, (?!.*?\])/,\n/g; s/^"/   "/gm' |\
perl -0pe 's/"/\\"/g; s/\n/\\n" + \n/g; s/^/"/gm; s/\}\\n.*$/}"/')

echo "String data = $newdata" >> /tmp/file.txt

The result is:

"{\n" +
"   \"array\": [\"v1\", \"v2\"],\n" +
"   \"5\": \">10%\",\n" +
"   \"4\": \"<5%\",\n" +
"   \"3\": \">28 months\",\n" +
"   \"2\": \"20%\",\n" +
"   \"1\": 100\n" +
"}"

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=158981&siteId=1