Skip to content

Instantly share code, notes, and snippets.

@mjpieters
Last active August 29, 2015 14:28
Show Gist options
  • Select an option

  • Save mjpieters/43f610d1b69f5bc6db73 to your computer and use it in GitHub Desktop.

Select an option

Save mjpieters/43f610d1b69f5bc6db73 to your computer and use it in GitHub Desktop.
>>> import re
>>> invalid_escape = re.compile(r'\\([1-3][0-7]{2}|[1-7][0-7]?)') # octal digits from 1 up to FF
>>> def replace_with_codepoint(match):
... return chr(int(match.group(0)[1:], 8))
...
>>> def repair(brokenjson):
... return invalid_escape.sub(replace_with_codepoint, brokenjson)
...
>>> sample = '{"destination":"Provence-Alpes-C\\303\\264te d\'Azur"}'
>>> repair(sample)
'{"destination":"Provence-Alpes-C\xc3\xb4te d\'Azur"}'
>>> json.loads(repair(sample))
{u'destination': u"Provence-Alpes-C\xf4te d'Azur"}
>>> print json.loads(repair(sample))['destination']
Provence-Alpes-Côte d'Azur
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment