Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: parsing JSON-like Javascript data structures (w/ consecutive commas)

I would like to parse JSON-like strings. Their lone difference with normal JSON is the presence of contiguous commas in arrays. When there are two such commas, it implicitly means that null should be inserted in-between. Example:

       JSON-like:  ["foo",,,"bar",[1,,3,4]]
      Javascript:  ["foo",null,null,"bar",[1,null,3,4]]
Decoded (Python):  ["foo", None, None, "bar", [1, None, 3, 4]]

The native json.JSONDecoder class doesn't allow me to change the behavior of the array parsing. I can only modify the parser for objects (dicts), ints, floats, strings (by giving kwargs functions to JSONDecoder(), please see the doc).

So, does it mean I have to write a JSON parser from scratch? The Python code of json is available but it's quite a mess. I would prefer to use its internals instead of duplicating its code!

like image 858
zopieux Avatar asked Jul 27 '26 06:07

zopieux


2 Answers

Since what you're trying to parse isn't JSON per se, but rather a different language that's very much like JSON, you may need your own parser.

Fortunately, this isn't as hard as it sounds. You can use a Python parser generator like pyparsing. JSON can be fully specified with a fairly simple context-free grammar (I found one here), so you should be able to modify it to fit your needs.

like image 99
Taymon Avatar answered Jul 29 '26 20:07

Taymon


Small & simple workaround to try out:

  1. Convert JSON-like data to strings.
  2. Replace ",," with ",null,".
  3. Convert it to whatever is your representation.
  4. Let JSONDecoder(), do the heavy lifting.

    1. & 3. can be omitted if you already deal with strings.

(And if converting to string is impractical, update your question with this info!)

like image 35
przemo_li Avatar answered Jul 29 '26 20:07

przemo_li



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!