Skip to content

feat(json-viewer): sort object keys alphabetically in read-only JSON trees - #1427

Merged
debba merged 1 commit into
TabularisDB:mainfrom
parthchilwerwar:feat/json-tree-key-sort
Nov 9, 2026
Merged

debba merged 1 commit into
TabularisDB:mainfrom
parthchilwerwar:feat/json-tree-key-sort

Conversation

@parthchilwerwar

@parthchilwerwar parthchilwerwar commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1394

Read-only JSON trees get a Sort keys toggle next to Expand all / Collapse all. When it is on, every object in the tree lists its properties alphabetically, at every level. Turning it off brings back the original order. It works in both layouts: the full-height JSON viewer and the compact tree in the row editor.

It changes only how the tree is shown:

  • Arrays keep their element order. Objects nested inside arrays are still sorted.
  • The value itself is untouched. The same object is passed to the editor, and onChange is never called.
  • It lasts only for the current session. The setting lives in component state and isn't saved anywhere.
  • Editable trees don't get the toggle, so a document you are editing always shows its own key order. If a sorted tree becomes editable, the sorting turns off.

Key order

The tree uses json-edit-react's own keySort prop; no new dependency. Its default comparison treats keys as plain strings, which puts "10" before "2". JavaScript already lists integer-like keys in numeric order, so turning that default on would have made objects keyed by id look worse than with the toggle off.

So keySort gets compareJsonKeys (in utils/jsonTree.ts), built on Intl.Collator with numeric: true:

  • numbers sort by value: 1, 2, 10;
  • numbers come before words;
  • words sort alphabetically, ignoring case on the first pass: apple, Banana, cherry.

UI

The toggle is a native button with aria-pressed, so screen readers announce its state. It uses the same pressed style as the JSON input's Diff and Side by side toggles and is disabled for a scalar root, just like Expand all and Collapse all. The jsonInput.sortKeys label is added to all 13 locales.

Testing

  • tests/utils/jsonTree.test.ts: four new tests.
    • compareJsonKeys orders numeric-looking keys, mixed-case words, and numbers before words.
    • One test renders the real JsonEditor with keySort={compareJsonKeys}. It checks that nested object keys are sorted (2, 10, list, …, nested, alpha, Beta, y, z) and that the array under list keeps its element order.
  • tests/components/ui/JsonTreeView.test.tsx: new sort keys tests.
    • In both layouts: the toggle turns keySort on and off, aria-pressed follows it, the data is passed through unchanged, and onChange is never called.
    • The toggle is absent in editable trees, and sorting stops when a read-only tree becomes editable.
    • The toggle can be reached with Tab and switched with Space.
    • It works for an empty object and an empty array, and is disabled for scalar roots.
  • tests/setup.ts: added ArrowDownAZ to the lucide-react mock.
  • Checks: pnpm typecheck passed. ESLint and Prettier passed on the changed files, and so did node scripts/check-theme-tokens.mjs.
  • Full web-ui suite on Windows: 7,925 passed and 16 skipped. Five tests in unrelated files failed during the full run: routing, EditorResultChart, VisualExplainPage, VisualExplainModal and NewConnectionModal. All 164 tests in those files pass when run on their own, on main and on this branch.

How to try it

  1. In SQLite, create a table without a primary key, so the JSON editor opens read-only:

    CREATE TABLE json_sort_demo (doc JSON);
    INSERT INTO json_sort_demo VALUES
      ('{"z":1,"10":"ten","2":"two","nested":{"y":2,"Beta":3,"alpha":4},"list":[{"d":1,"c":2},"x"]}');
  2. Open the table, right-click the doc cell and choose Open in JSON Editor, then switch to Tree.

  3. Click Sort keys. The keys show as 2, 10, list, nested, z, with alpha, Beta, y inside nested and c, d inside the first array item. The array items stay in order: the object first, then "x".

  4. Click it again. The original order 2, 10, z, nested, list comes back.

  5. Open a table with a primary key in the JSON editor. The tree is editable there, and there is no Sort keys button.


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

…trees

Add a Sort keys toggle to read-only JSON trees, next to Expand all and
Collapse all, in both the compact and full-height layouts. It passes
json-edit-react's keySort a numeric-aware collator, so "2" sorts before
"10" and case is ignored at the first pass. Arrays keep their element
order, the value and onChange are untouched, and editable trees do not
offer the toggle.

Closes TabularisDB#1394
Copilot AI balanced review requested due to automatic review settings November 7, 2026 09:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@kilo-code-bot

kilo-code-bot Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (18 files)
  • packages/web-ui/src/components/ui/JsonTreeView.tsx
  • packages/web-ui/src/utils/jsonTree.ts
  • packages/web-ui/src/i18n/locales/*.json (13 locales)
  • packages/web-ui/tests/components/ui/JsonTreeView.test.tsx
  • packages/web-ui/tests/setup.ts
  • packages/web-ui/tests/utils/jsonTree.test.ts

Reviewed by claude-sonnet-5.5 · Input: 248 · Output: 1.4K · Cached: 200.1K

Review guidance: REVIEW.md from base branch main

@1-DARK 1-DARK left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for a very thorough PR.

I verified it locally on macOS against main (c7ce199):

  • The 16 new tests pass (74 vs 58 on main for the touched files).
  • pnpm typecheck, ESLint, Prettier and check-theme-tokens.mjs are clean.
  • All 13 locales have jsonInput.sortKeys.
  • A full canonical suite run came back green: 499 files / 8,159 tests.
  • keySort?: boolean | CompareFunction confirmed in [email protected] - no new dependency, as described.
  • Manually exercised against a real SQLite DB: sorting is recursive, arrays keep element order, disabling restores the original order, the toggle is absent on editable trees and disabled for scalar roots, and Tab + Space work with aria-pressed announced.

LGTM - approving.

@debba debba left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for using the JSON editor's display sorting without replacing the underlying value, and for covering nested objects, array order and both viewer layouts.

Thanks for your first PR here! We would be happy to see you on Discord if you would like to join in: https://discord.com/invite/K2hmhfHRSt

All 74 targeted frontend tests pass on this branch, and typecheck passes.

I haven't found a blocking issue in the diff. I'll run end-to-end tests for a final verification, then proceed with the merge if those checks pass.

@debba

debba commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

I tested the read-only JSON viewer from a PostgreSQL materialized view in the real web app. Nested keys sort correctly, numeric keys keep 2 before 10, array elements retain their order, and switching sorting off restores the original tree. The raw JSON stays unchanged. The editable compact tree in the SQLite row editor correctly hides Sort keys. It works and is ready to merge. Lint, typecheck and all 8,159 frontend tests passed. The unchanged Rust tree matches the one tested for #1443; host test:all has the same GLib setup failure as main. Compact read-only and scalar-root cases were covered by unit tests, not manual app checks; desktop was not tested.

@debba
debba merged commit 9a68d21 into TabularisDB:main Nov 9, 2026
3 checks passed
@parthchilwerwar

Copy link
Copy Markdown
Contributor Author

A late thank you, @debba and @1-DARK, for the reviews and for testing it in the web app. And thanks for the Discord invite.

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.

[Feat]: JSON viewer: toggle alphabetical object-key ordering

4 participants