Is there any way to make simplejson less strict?
Asked Answered
K

3

6

I'm interested in having simplejson.loads() successfully parse the following:

{foo:3}

It throws a JSONDecodeError saying "expecting property name" but in reality it's saying "I require double quotes around my property names". This is annoying for my use case, and I'd prefer a less strict behavior. I've read the docs, but beyond making my own decoder class, I don't see anything obvious that changes this behavior.

Kallman answered 1/2, 2012 at 23:23 Comment(5)
So you want simplejson accept something that's not valid JSON? (It's JavaScript where the quotes are optional. In JSON they're mandatory.)Thickskinned
Yup, that's exactly what I want. :)Kallman
What I was implying is "this is something you shouldn't want in the first place."Thickskinned
Yeah, I know it's not JSON, but what I'm parsing is written by a person, so I want to be lenient. The YAML solution (below) is great.Kallman
YAML is probably the better option if you want human authoring, yes.Thickskinned
S
11

You can use YAML (>=1.2)as it is a superset of JSON, you can do:

>>> import yaml
>>> s = '{foo: 8}'
>>> yaml.load(s)
{'foo': 8}
Socratic answered 1/2, 2012 at 23:33 Comment(6)
@Kallman I just noticed that yaml needs a space after colon.I think this will spoil your party.Socratic
It does, unless the RHS is an array. {foo:[bar]} is fine, which is actually what I'm doing.Kallman
As RanRag said, yaml will fail if there is no space after colon, for example: yaml.load('{a:"b"}')Impalpable
@suud: i still don't understand the downvote because I have clearly mentioned that yaml will fail in that particular scenario and OP was fine with it. So, why the downvote?Socratic
Sometimes a question in SO despite looks like a specific question, it can be represented as general question, that's why you can see some questions here are marked as duplicated question. I have same question with the OP and I downvoted your answer because your solution is not perfect. Is it clear enough? :)Impalpable
@suud : I got your point. Can you please share the perfect solution for this ?Socratic
I
2

You can try demjson.

>>> import demjson
>>> demjson.decode('{foo:3}')
{u'foo': 3}
Impalpable answered 26/11, 2014 at 17:39 Comment(0)
G
1

No, this is not possible. To successfully parse that using simplejson you would first need to transform it into a valid JSON string.

Depending on how strict the format of your incoming string is this could be pretty simple or extremely complex.

For a simple case, if you will always have a JSON object that only has letters and underscores in keys (without quotes) and integers as values, you could use the following to transform it into valid JSON:

import re
your_string = re.sub(r'([a-zA-Z_]+)', r'"\1"', your_string)

For example:

>>> re.sub(r'([a-zA-Z_]+)', r'"\1"', '{foo:3, bar:4}')
'{"foo":3, "bar":4}'
Gunnel answered 1/2, 2012 at 23:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.