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

Output mode JSON should either be an Array or not use trailing commas #483

Closed
fzakaria opened this issue Sep 5, 2023 · 2 comments
Closed
Assignees

Comments

@fzakaria
Copy link

fzakaria commented Sep 5, 2023

The json output mode emits trailing commas which makes it difficult to parse with many standard JSON tools like jq or python itself.

Here is me generating the list:

❯ sqlelf /usr/bin/ruby --sql ".mode json" --sql "select path,name from elf_sections;" > test.json

❯ head test.json
{ "path": "\/usr\/bin\/ruby", "name": ""},
{ "path": "\/usr\/bin\/ruby", "name": ".interp"},
{ "path": "\/usr\/bin\/ruby", "name": ".note.gnu.property"},
{ "path": "\/usr\/bin\/ruby", "name": ".note.gnu.build-id"},
{ "path": "\/usr\/bin\/ruby", "name": ".note.ABI-tag"},
{ "path": "\/usr\/bin\/ruby", "name": ".gnu.hash"},
{ "path": "\/usr\/bin\/ruby", "name": ".dynsym"},
{ "path": "\/usr\/bin\/ruby", "name": ".dynstr"},
{ "path": "\/usr\/bin\/ruby", "name": ".gnu.version"},
{ "path": "\/usr\/bin\/ruby", "name": ".gnu.version_r"},

Failing with JQ:

❯ cat test.json | jq
{
  "path": "/usr/bin/ruby",
  "name": ""
}
parse error: Expected value before ',' at line 1, column 42

Failing with Python:

❯ python
Python 3.10.12 (main, Jun  6 2023, 22:43:10) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> f = open('test.json', 'r')
>>> data = json.load(f)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/nix/store/bc45k1n0pkrdkr3xa6w84w1xhkl1kkyp-python3-3.10.12/lib/python3.10/json/__init__.py", line 293, in load
    return loads(fp.read(),
  File "/nix/store/bc45k1n0pkrdkr3xa6w84w1xhkl1kkyp-python3-3.10.12/lib/python3.10/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/nix/store/bc45k1n0pkrdkr3xa6w84w1xhkl1kkyp-python3-3.10.12/lib/python3.10/json/decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 42 (char 41)

I think this can be remedied by wrapping the row results in brackets to make them a list which would make it compliant.

@rogerbinns rogerbinns self-assigned this Sep 5, 2023
@rogerbinns
Copy link
Owner

rogerbinns commented Sep 5, 2023

There is reason and it was reason from almost 20 years ago, when the SQLite shell didn't have JSON output and jsonl wasn't a thing yet. The reason back then was anticipating wanting to join the output of multiple queries which is why use JSON and not a different format, since it would allow each query result to have different columns, and then have manual [ / ] put around it. Anyway that reason is very bogus now.

The fix is:

  • Make mode json output the [ / ] around the results each time
  • Add mode jsonl which does newline delimited json

@fzakaria
Copy link
Author

fzakaria commented Sep 5, 2023

Sounds reasonable.

rogerbinns added a commit that referenced this issue Sep 12, 2023
* json does an array including opening and closing square
  brackets, comma separation
* jsonl is newline separated

Works by inspection, tests still need updating

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

No branches or pull requests

2 participants