Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Your regex that separates JSON objects is buggy #62

Closed
ctrlcctrlv opened this issue Jun 23, 2022 · 3 comments
Closed

Your regex that separates JSON objects is buggy #62

ctrlcctrlv opened this issue Jun 23, 2022 · 3 comments

Comments

@ctrlcctrlv
Copy link

Because JSON strings may well contain }.

fred@mapache:/tmp# jq <Unicode.mediawiki.json|genson
Traceback (most recent call last):
  File "/home/fred/.local/bin/genson", line 8, in <module>
    sys.exit(main())
  File "/home/fred/.local/lib/python3.10/site-packages/genson/cli.py", line 9, in main
    CLI().run()
  File "/home/fred/.local/lib/python3.10/site-packages/genson/cli.py", line 22, in run
    self.add_objects()
  File "/home/fred/.local/lib/python3.10/site-packages/genson/cli.py", line 32, in add_objects
    self._call_with_json_from_fp(self.builder.add_object, fp)
  File "/home/fred/.local/lib/python3.10/site-packages/genson/cli.py", line 125, in _call_with_json_from_fp
    method(json.loads(json_string))
  File "/usr/lib/python3.10/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.10/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.10/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: "Unterminated string starting at: line 26 column 16 (char 438)"
fred@mapache:/tmp# jq .[] -c <Unicode.mediawiki.json|genson
Traceback (most recent call last):
  File "/home/fred/.local/bin/genson", line 8, in <module>
    sys.exit(main())
  File "/home/fred/.local/lib/python3.10/site-packages/genson/cli.py", line 9, in main
    CLI().run()
  File "/home/fred/.local/lib/python3.10/site-packages/genson/cli.py", line 22, in run
    self.add_objects()
  File "/home/fred/.local/lib/python3.10/site-packages/genson/cli.py", line 32, in add_objects
    self._call_with_json_from_fp(self.builder.add_object, fp)
  File "/home/fred/.local/lib/python3.10/site-packages/genson/cli.py", line 125, in _call_with_json_from_fp
    method(json.loads(json_string))
  File "/usr/lib/python3.10/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.10/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.10/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: "Unterminated string starting at: line 1 column 230 (char 229)"

I needed:

jq .[] -c < $FILE | genson --delimiter 'newline' | jq

Finally yielding:

{
  "$schema": "http://json-schema.org/schema#",
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "revid": {
        "type": "integer"
      },
      "parentid": {
        "type": "integer"
      },
      "minor": {
        "type": "string"
      },
      "user": {
        "type": "string"
      },
      "timestamp": {
        "type": "array",
        "items": {
          "type": "integer"
        }
      },
      "comment": {
        "type": "string"
      },
      "slots": {
        "type": "object",
        "properties": {
          "main": {
            "type": "object",
            "properties": {
              "contentmodel": {
                "type": "string"
              },
              "contentformat": {
                "type": "string"
              },
              "*": {
                "type": "string"
              }
            },
            "required": [
              "*",
              "contentformat",
              "contentmodel"
            ]
          }
        },
        "required": [
          "main"
        ]
      },
      "anon": {
        "type": "string"
      },
      "commenthidden": {
        "type": "string"
      }
    }
  }
}
@wolverdude
Copy link
Owner

This is a known issue (#23).

GenSON trying to guess if a file contains multiple JSON objects is a "feature" that probably shouldn't have been added. I'll try to address this in the next release by removing that as the default behavior and requiring the use of a --delimeter if multi-object parsing is needed.

@ctrlcctrlv
Copy link
Author

Sorry

@wolverdude
Copy link
Owner

No need to apologize. Knowing that you also ran into this issue is important. I appreciate the feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants