1

I am getting JSON data from a hardware device in a Python script. The JSON data is not 100% valid: some base 10 integers have leading zeros, which is apparently forbidden. This makes the Python JSON module throw an exception.

Is there any way to ask the parser to be more permissive? Or am I condemned to do all the parsing myself?

I have already sent a mail to the hardware device manufacturer but they don't seem to care...

galinette
  • 8,896
  • 2
  • 36
  • 87
  • 2
    can you show the code and the data you are getting from the device – babygame0ver Dec 16 '17 at 13:34
  • @babygame0ver : put any integer value with a leading 0, this will throw an exception with the python parser. This is known and expected behavior, see https://stackoverflow.com/questions/27361565/why-is-json-invalid-if-an-integer-begins-with-0 for instance – galinette Dec 21 '17 at 21:10
  • Python 3. I have tested also with Qt JSON parser and this is the same. This is expected behavior as it's clearly forbidden by JSON format. The question is not why this happens, but how to deal with that bad JSON data which is sent by a device without rewriting a parser. – galinette Dec 21 '17 at 21:12

1 Answers1

0

According to the documentation for JSONDecoder,

parse_int, if specified, will be called with the string of every JSON int to be decoded. By default, this is equivalent to int(num_str). This can be used to use another datatype or parser for JSON integers (e.g. float).

So, just write some code that handles leading zeros. (Note: In C a leading zero indicates that the number is an octal literal. Since you're mentioning a hardware device, you should confirm whether the numbers you are receiving are base 8 or 10 or 16 or 2 or ...?)

def parse_00int(strval):
    stripped = strval.lstrip('0')
    return 0 if stripped == '' else int(stripped)
aghast
  • 14,785
  • 3
  • 24
  • 56
  • If the json is of the form `'{"foo": 00003}'` then you'll get an exception (`json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 10 (char 9)`) regardless (in python3). – snakecharmerb Dec 16 '17 at 14:44
  • I don't fully understand, is it possible to replace the int parsing function used by JSON in order to make this work? – galinette Dec 21 '17 at 21:16
  • You would have to change NUMBER_RE in https://github.com/python/cpython/blob/3.6/Lib/json/scanner.py#L11 – Josh Lee Dec 21 '17 at 21:25