File size: 327 Bytes
42da440
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import json


def escape_string_for_json(string: str) -> str:
    escaped_string = json.dumps(string)

    return escaped_string[1:-1]


# Test the function
if __name__ == "__main__":
    test_str = 'This is a "test" [string] {} with some \\"escaped\\" quotes and "unescaped" ones.'
    print(escape_string_for_json(test_str))