Python can't parse JSON with extra trailing comma

前端 未结 6 903
梦如初夏
梦如初夏 2020-12-21 03:59

This code:

import json
s = \'{ \"key1\": \"value1\", \"key2\": \"value2\", }\'
json.loads(s)

produces this error in Python 2:

6条回答
  •  清歌不尽
    2020-12-21 04:15

    JSON specification doesn't allow trailing comma. The parser is throwing since it encounters invalid syntax token.

    You might be interested in using a different parser for those files, eg. a parser built for JSON5 spec which allows such syntax.

提交回复
热议问题