Lenient JSON Parser for Python

pep*_*uan 6 python json

Is there a "lenient" JSON Parser for Python?

I keep getting (handwritten) JSON files such as this:

/* This JSON file is created by someone who does not know JSON
   And not competent enough to search about "JSON Validators" */

{

  /* Hey look!
     A honkin' block comment here!
     Yeehaw */

  "key1": "value1",  // Hey look there's a standard-breaking comment here!
  "key3": .65,       // I'm too lazy to type "0"
  "key4": -.75,      // That "other" .Net program works anyways...
  "key5": [ 1 /* One */, 2 /* Two */, 3 /* Three */, 4 /* Four */],
  "key2": "value2",  // Whoopsie, forgot to delete the comma here...
}
Run Code Online (Sandbox Code Playgroud)

The program that actually consumed those monstrously malformed JSON files somehow doesn't puke on those errors. That program is written using C#, by the way.

I'm writing some scripts in Python that will perform things based on those JSON files, but it keeps crashing (correctly) on those mistakes.

I can manually edit those .json files to be standard-compliant... but there are a LOT of them and thus it's too effort-intensive -- not to mention that I will have to keep editing new incoming JSON files, urgh.

So, back to my question, is there a lenient JSON parser that can consume those malformed JSON files without dying?

Note: This question concerns only trailing comma of last object; it does NOT handle block-comments and/or inline comments.


Edit: What the... I just received a JSON file in which the creator decided to remove leading zero for 0 < numbers < 1 ... -_-

And I discovered a file where the comment is embedded... :fuming_red:

I'll update the example above to reflect my additional "findings"...

pep*_*uan 2

好吧,@warl0ck 的评论让我觉得我最好编写自己的“JSON 预处理器”来进行繁重的清理工作。

因此,它位于我的BitBucket Snippet中,并完成了简单的单元测试。

我已经用我的人类生成的格式错误的 JSON 文件的语料库对其进行了测试,到目前为止它似乎运行良好......

让我知道代码中是否有错误。

但就目前而言,我很满足。


编辑:因为 BitBucket 删除了我的所有片段,所以我将代码重新上传到 GitHub:https ://gist.github.com/pepoluan/361724bfa5cce9d863dadc6e2bdcb8c9