'How to pass strings that may be encoded multiple times

I need to pass data around that can be encoded in multiple data formats. For instance I have json that includes a bash script to be run. This json data may be passed around back and forth through multiple services and I need to make sure that they don't get double escaped. Additionally, this bash script may contain a json input as well (e.g. Json -> Bash -> Json)

I was thinking that perhaps I could include something that states how each piece of of data should be encoded and decoded. Then fully encode and decode it every time it passes through a service. However, that seems clumsy, brittle and likely to have performance issues.

Any other ideas?

Here is an example of what the input might look like:


{
  "bash-script": "echo '{\\\"foo\\\": \\\"bar\\\"}'"
}



Solution 1:[1]

You could keep unescaping while the JSON is valid, since a valid JSON is not a valid bash script except for one single command surrounded by string.

def unescape_all(s):
    while True:
        try:
            s = json.loads(s)
        except json.JSONDecodeError:
            return s

Example

codes = ['echo "Hello world"', 
        '"echo \\"Hello world\\""', 
        '"\\"echo \\\\\\"Hello world\\\\\\"\\""', 
        '"\\"\\\\\\"echo \\\\\\\\\\\\\\"Hello world\\\\\\\\\\\\\\"\\\\\\"\\""']
print([unescape_all(s) for s in codes])

and you see

['echo "Hello world"', 'echo "Hello world"', 'echo "Hello world"', 'echo "Hello world"']

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bob