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": 100n" +
"}"
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