Skip to content

Fix JSON output: omit null values and add trailing newline#149

Merged
amc-corey-cox merged 2 commits intomainfrom
fix-json-output
Mar 10, 2026
Merged

Fix JSON output: omit null values and add trailing newline#149
amc-corey-cox merged 2 commits intomainfrom
fix-json-output

Conversation

@amc-corey-cox
Copy link
Copy Markdown
Contributor

Summary

Changes

  • Added _strip_nulls() helper in output_streams.py that recursively removes None values from dicts before JSON serialization
  • Applied to all JSON/JSONL code paths: dump_output(), JSONStreamWriter, JSONLStreamWriter, json_stream(), jsonl_stream()
  • Added trailing \n to JSON/JSONL output in dump_output() (streaming paths already had this)

Test plan

  • 5 new tests in test_output_streams.py — null suppression for all JSON/JSONL writers + nested nulls
  • 3 new tests in test_cli.py — trailing newline and null suppression via dump_output()
  • Full test suite passes (493 passed, 4 skipped)

🤖 Generated with Claude Code

Fixes #139 (slot suppression not working in JSON) and #141 (missing
final newline). JSON/JSONL output now strips None values to match
YAML behavior, and single-object JSON output ends with a newline.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aligns JSON/JSONL output behavior with existing YAML behavior by omitting None-valued keys, and ensures JSON/JSONL CLI output ends with a trailing newline (fixing console prompt “run-on” and Unix text conventions).

Changes:

  • Add _strip_nulls() helper to recursively remove None values from dictionaries prior to JSON/JSONL serialization.
  • Apply null-stripping across all JSON/JSONL stream writer code paths (JSONStreamWriter, JSONLStreamWriter, json_stream, jsonl_stream) and CLI dump_output().
  • Ensure dump_output() JSON and JSONL output always ends with \n, and add tests covering null suppression + newline behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
src/linkml_map/writers/output_streams.py Adds _strip_nulls() and applies it to all JSON/JSONL streaming serialization paths.
src/linkml_map/cli/cli.py Updates dump_output() JSON/JSONL branches to strip nulls and append trailing newline.
tests/test_writers/test_output_streams.py Adds unit tests asserting JSON/JSONL streaming omits None keys (including nested dicts).
tests/test_cli/test_cli.py Adds CLI-level tests for trailing newline and null suppression in dump_output().
Comments suppressed due to low confidence (1)

tests/test_cli/test_cli.py:7

  • The standard-library imports in this file are now out of order (adding import json before from collections.abc ... breaks Ruff/isort sorting). Please reorder the import block to match the project’s isort configuration so linting passes.
"""Tests all command-line subcommands."""

import json
from collections.abc import Generator
from pathlib import Path
import re
from typing import Any, Optional, Union

@amc-corey-cox amc-corey-cox merged commit 88c6598 into main Mar 10, 2026
7 checks passed
@amc-corey-cox amc-corey-cox deleted the fix-json-output branch March 10, 2026 21:23
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

Successfully merging this pull request may close these issues.

Missing final newline character in JSON output Slot suppression doesn't work with JSON output serialisation

2 participants