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

Invalid JSON with multiple top-level objects #2902

Open
viktormalik opened this issue Jan 5, 2024 · 1 comment
Open

Invalid JSON with multiple top-level objects #2902

viktormalik opened this issue Jan 5, 2024 · 1 comment
Labels

Comments

@viktormalik
Copy link
Contributor

When printing multiple values (e.g. maps) in JSON format, we get a JSON doc with multiple top-level objects which is not a valid JSON according to the standards.

What reproduces the bug? Provide code if possible.

Consider this bpftrace program printing 2 maps and the info about attached probes:

# bpftrace -f json -e 'BEGIN { @x = 1; @y = 2; exit(); }'
{"type": "attached_probes", "data": {"probes": 1}}
{"type": "map", "data": {"@x": 1}}
{"type": "map", "data": {"@y": 2}}

Trying to parse it with Python's JSON parser throws an exception:

# bpftrace -f json -e 'BEGIN { @x = 1; @y = 2; exit(); }' | python3 -c "import json, sys; json.loads(sys.stdin.read())"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib64/python3.12/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.12/json/decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 4 column 1 (char 53)

To comply with the standard, the output should be:

[
  {"type": "attached_probes", "data": {"probes": 1}},
  {"type": "map", "data": {"@x": 1}},
  {"type": "map", "data": {"@y": 2}}
]
@danobi
Copy link
Member

danobi commented Mar 12, 2024

As discussed in office hours, it seems we've inadvertently implemented ndjson (https://github.com/ndjson/ndjson-spec). Since ndjson is a real thing, perhaps the test runner should just properly support it

jordalgo pushed a commit to jordalgo/bpftrace that referenced this issue Mar 15, 2024
bpftrace can output multiple lines of valid json
instead of one large valid json blob. This adds
test runner support to check multiple json lines.

Related issue:
bpftrace#2902
jordalgo pushed a commit to jordalgo/bpftrace that referenced this issue Mar 15, 2024
bpftrace can output multiple lines of valid json
instead of one large valid json blob. This adds
test runner support to check multiple json lines.

Added a test for multiple map json output as an
example.

Related issue:
bpftrace#2902
jordalgo pushed a commit to jordalgo/bpftrace that referenced this issue Mar 15, 2024
bpftrace can output multiple lines of valid json
instead of one large valid json blob. This adds
test runner support to check multiple json lines.

Added a test for multiple map json output as an
example.

Related issue:
bpftrace#2902
jordalgo pushed a commit to jordalgo/bpftrace that referenced this issue Mar 15, 2024
bpftrace can output multiple lines of valid json
instead of one large valid json blob. This adds
test runner support to check multiple json lines.

Added a test for multiple map json output as an
example.

Related issue:
bpftrace#2902
jordalgo pushed a commit to jordalgo/bpftrace that referenced this issue Mar 18, 2024
bpftrace can output multiple lines of valid json
instead of one large valid json blob. This adds
test runner support to check multiple json lines.

- Added a test for multiple map json output as an
example.
- Fixed a currently flakey test: histogram-finegrain

Related issues:
bpftrace#2902
bpftrace#3035
jordalgo pushed a commit to jordalgo/bpftrace that referenced this issue Mar 19, 2024
bpftrace can output multiple lines of valid json
instead of one large valid json blob. This adds
test runner support to check multiple json lines.

- Added a test for multiple map json output as an
example.
- Fixed a currently flakey test: histogram-finegrain

Related issues:
bpftrace#2902
bpftrace#3035
jordalgo pushed a commit to jordalgo/bpftrace that referenced this issue Mar 19, 2024
bpftrace can output multiple lines of valid json
instead of one large valid json blob. This adds
test runner support to check multiple json lines.

- Added a test for multiple map json output as an
example.
- Fixed a currently flakey test: histogram-finegrain

Related issues:
bpftrace#2902
bpftrace#3035
jordalgo added a commit that referenced this issue Mar 21, 2024
bpftrace can output multiple lines of valid json
instead of one large valid json blob. This adds
test runner support to check multiple json lines.

- Added a test for multiple map json output as an
example.
- Fixed a currently flakey test: histogram-finegrain

Related issues:
#2902
#3035

Co-authored-by: Jordan Rome <jordalgo@fedoraproject.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants