This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: json library needs a non-strict option to decode single-quoted strings
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, mu_mind, pitrou, rhettinger, serhiy.storchaka
Priority: normal Keywords:

Created on 2014-01-19 02:19 by mu_mind, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg208430 - (view) Author: David Barnett (mu_mind) Date: 2014-01-19 02:19
Many sloppy JSON APIs return data with poorly-encoded strings, either with single-quotes as delimiters:
  {'foo': 'bar'}
or with no delimiters at all on keys:
  {foo: 'bar'}

The json library is useless for making sense of this data, because all it will tell you is "No JSON object could be decoded".

It would be incredibly helpful if the json library had some special non-strict decoding mode that could interpret these common misspellings of JSON strings. Or, more generally, it could accept another decoder hook to reformat the remaining string, something like:
  def malformed_hook(remaining, parent):
      if remaining.startswith("'"):
          # We know this is a string, regex it to find the end.
          m = re.match(pyparsing.quotedString.reString, remaining)
          if m is not None:
              # Use json.dumps to add quotes around string literal.
              return json.dumps(eval(m.group(0))) + remaining[m.end():]
      # If we're inside an object, this could be a naked object key.
      if isinstance(parent, dict):
          m = re.match(r'([a-zA-Z_]\w*):', remaining)
          if m is not None:
              return json.dumps(m.group(1)) + remaining[len(m.group(1)):]
  print(json.loads("['foo', null, {a: 'b'}]", malformed_hook=malformed_hook))
  ['foo', None, {'a': 'b'}]

This would at least save you having to write a parser/tokenizer from scratch.
msg208479 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-01-19 13:53
I think this is just not JSON. Try to use YAML parsers.
msg208614 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-01-21 05:14
Serhiy is correct.  We're trying to stick close to the JSON spec.
History
Date User Action Args
2022-04-11 14:57:57adminsetgithub: 64497
2014-01-21 05:14:38rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg208614
2014-01-19 13:53:13serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg208479
2014-01-19 13:31:53serhiy.storchakasetnosy: + rhettinger, pitrou, ezio.melotti

versions: + Python 3.5
2014-01-19 02:19:14mu_mindcreate