Python: parsing JSON-like Javascript data structures (w/ consecutive commas)

前端 未结 6 1826
南旧
南旧 2020-12-10 22:55

I would like to parse JSON-like strings. Their lone difference with normal JSON is the presence of contiguous commas in arrays. When there are two such commas, it i

6条回答
  •  旧时难觅i
    2020-12-10 23:06

    It's a hackish way of doing it, but one solution is to simply do some string modification on the JSON-ish data to get it in line before parsing it.

    import re
    import json
    
    not_quite_json = '["foo",,,"bar",[1,,3,4]]'
    not_json = True
    while not_json:
        not_quite_json, not_json = re.subn(r',\s*,', ', null, ', not_quite_json)
    

    Which leaves us with:

    '["foo", null, null, "bar",[1, null, 3,4]]'
    

    We can then do:

    json.loads(not_quite_json)
    

    Giving us:

    ['foo', None, None, 'bar', [1, None, 3, 4]]
    

    Note that it's not as simple as a replace, as the replacement also inserts commas that can need replacing. Given this, you have to loop through until no more replacements can be made. Here I have used a simple regex to do the job.

提交回复
热议问题