Relaxed, or best-efforts JSON parser for Python?

MRAB python at mrabarnett.plus.com
Sun Oct 11 22:43:57 EDT 2015


On 2015年10月12日 01:56, Victor Hooi wrote:
> Hi,
>> I'm attempting to parse MongoDB loglines.
>> The formatting of these loglines could best be described as JSON-like...
>> For example - arrays
>> Anyhow, say I had the following logline snippet:
>> { Global: { acquireCount: { r: 2, w: 2 } }, Database: { acquireCount: { w: 2 } }, Collection: { acquireCount: { w: 1 } }, oplog: { acquireCount: { w: 1 } } }
>> This won't parse with json.loads() - the main issues is the missing quotation marks (") around the strings.
>> My question, is there a more lenient, or relaxed JSON parser available for Python, that will try to do a best-efforts parsing of non-spec JSON?
>Have you tried first adding the quotes using the re module?
 >>> import json, re
 >>> line = '{ Global: { acquireCount: { r: 2, w: 2 } }, Database: { 
acquireCount: { w: 2 } }, Collection: { acquireCount: { w: 1 } }, oplog: 
{ acquireCount: { w: 1 } } }'
 >>> json.loads(re.sub(r'(\w+)', r'"1円"', line))


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /