0

I got a Javascript regex to repair broken JSON-Objects (my backend removes all quotes from the JSON string, the regex adds them again).

var src = '[{ key: any text with spaces, emptykey: ,  foo: 0}, { key2: other text with spaces, emptykey2: ,  foo2: 2},]';

console.log(src.replace(/(\w+):(\s*)(.*?)(,|})/g, '"$1":$2"$3"$4'));
// outputs [{ "key" : "any text with spaces", emptykey: "", "foo": "0"},...]

I need to translate this regex replace to python but I don't know how to include the part with named back references. Here is my starting point

    import json
    import re

    invalid_json = '[{ key: any text with spaces, emptykey: ,  foo: 0}, { key2: other text with spaces, emptykey2: ,  foo2: 2}]'
    result = re.sub('/(\w+):(\s*)(.*?)(,|})/g', what to do here in python?, invalid_json)
    print result
3
  • 1
    @MohammadYusufGhazi how would I use the javascript placeholders $1 in python? Commented Dec 15, 2016 at 10:40
  • 1
    Replace $1 by \\1 Commented Dec 15, 2016 at 10:41
  • @ManuKaracho r'\1' or '\\1' Commented Dec 15, 2016 at 10:46

1 Answer 1

3
import json
import re

invalid_json = '[{ key: any text with spaces, emptykey: ,  foo: 0}, { key2: other text with spaces, emptykey2: ,  foo2: 2}]'
result = re.sub('(\w+):(\s*)(.*?)(,|})', r'"\1":\2"\3"\4', invalid_json)
print result
print json.loads(result)

Output:

[{ "key": "any text with spaces", "emptykey": "",  "foo": "0"}, { "key2": "other text with spaces", "emptykey2": "",  "foo2": "2"}]
[{u'emptykey': u'', u'foo': u'0', u'key': u'any text with spaces'}, {u'key2': u'other text with spaces', u'emptykey2': u'', u'foo2': u'2'}]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.