ManuKaracho
ManuKaracho

Reputation: 1218

Translating Javascript regex to Python

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

Upvotes: 0

Views: 497

Answers (1)

Mohammad Yusuf
Mohammad Yusuf

Reputation: 17064

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'}]

Upvotes: 3

Related Questions