xq icon indicating copy to clipboard operation
xq copied to clipboard

--json: self-closing tags have null content

Open bukzor opened this issue 2 months ago • 1 comments

Summary

Empty XML elements now output as null in JSON instead of empty objects {}.

This makes the JSON formatter consistent with the XML formatter's treatment of empty elements.

Problem

The XML formatter already treats empty elements as semantically null by collapsing them to self-closing tags:

echo '<root><empty>  </empty></root>' | xq
# Output: <root><empty/></root>

But the JSON formatter was inconsistent, treating them as empty containers:

echo '<root><empty/></root>' | xq -j
# Before: {"root": {"empty": {}}}

Solution

Self-closing tags (and equivalent forms) now have null content in JSON:

echo '<root><empty/></root>' | xq -j
# After: {"root": {"empty": null}}

This applies to:

  • Self-closing: <tag/>
  • Immediate-closing: <tag></tag>
  • Whitespace-only: <tag> </tag>

Changes

  • Modified nodeToJSONInternal() to return nil for empty elements
  • Added tests:
    • TestSelfClosingTagsHaveNullContent - establishes JSON behavior
    • TestWhitespaceOnlyTagsAreSelfClosing - establishes XML formatter behavior

Breaking Change

This is a breaking change for users who:

  • Check typeof(value) == "object" on potentially empty elements
  • Iterate over empty objects

However, {} was semantically incorrect - it implied a container that could have had properties, when xq had already decided (via XML formatting) that these elements are truly empty.

🤖 Generated with Claude Code

bukzor avatar Nov 14 '25 22:11 bukzor

:warning: Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 80.63%. Comparing base (ec5a59a) to head (228ca6d). :exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #166      +/-   ##
==========================================
+ Coverage   80.57%   80.63%   +0.05%     
==========================================
  Files           5        5              
  Lines         690      692       +2     
==========================================
+ Hits          556      558       +2     
  Misses         92       92              
  Partials       42       42              

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov-commenter avatar Nov 14 '25 22:11 codecov-commenter