feat(metadata,cli,frontend): accept { "trials": [...] } JSON wrapper#116
Open
Mandyx22 wants to merge 1 commit into
Open
feat(metadata,cli,frontend): accept { "trials": [...] } JSON wrapper#116Mandyx22 wants to merge 1 commit into
Mandyx22 wants to merge 1 commit into
Conversation
Some jsPsych exports (e.g. from OSF) wrap the trials array as a single
JSON object `{ "trials": [...] }` instead of a bare array. Every consumer
parsed JSON then checked `Array.isArray`, so these files were silently
skipped ("0 files read") by the CLI/frontend and threw in the library.
Add a shared `unwrapTrials` helper (exported from @jspsych/metadata) that
unwraps the array only for an exact single-key `{ trials: [...] }` wrapper
and returns every other shape unchanged, so existing gates keep their
behavior. An object with sibling keys (`{ trials, meta }`) is left
untouched rather than discarding top-level metadata.
generate(), the CLI directory pipeline, and the frontend uploader all
route JSON through the helper. A wrapped file is converted to a Psych-DS
data CSV (plus sidecars) and its literal wrapped original is still
preserved under data/raw/.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: 02ca494 The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What & why
Some jsPsych data exports (e.g. from OSF) wrap the trials array as a single JSON object
{ "trials": [ … ] }instead of the bare array the pipeline expects. Every consumer parsed JSON then checkedArray.isArray, so these files were silently skipped by the CLI/frontend (0 files read) and threw in the library — a confusing failure with no real error.This makes all three packages accept the wrapper by unwrapping it before processing.
Approach
One shared helper,
unwrapTrials, exported from@jspsych/metadataand used at every JSON parse site:{ trials: [...] }wrapper.{ trials: [...], meta: {...} }) is deliberately left untouched rather than silently discarding top-level metadata.Wired into:
packages/metadata/src/index.ts—generate()(throw preserved)packages/cli/src/data.ts— pre-analysis, dry-run, and writer pathspackages/frontend/src/pages/DataUpload.tsx— join-key preflight + CSV writerA wrapped file is converted to a Psych-DS data CSV (plus sidecars), and its literal wrapped original is still preserved byte-for-byte under
data/raw/.Tests — 339 pass across all 23 suites
packages/metadata/tests/unwrap-trials.test.ts— helper edge cases (empty trials, non-arraytrials, sibling keys, null/primitive, pre-parsed input, malformed) +generate()wrapper-vs-bare-array equivalence + still-throws on non-array.packages/cli/tests/data.test.ts— wrapper → main CSV from unwrapped trials and byte-identical raw preservation; pre-analysis no longer skips;{ trials: {} }still skipped.packages/frontend/tests/unwrapTrials.test.ts— guards the frontend's import/contract with the helper. (A fullDataUploadrender test was intentionally skipped:runGeneratecallsgenerate(), which fetches plugins over the network — something no frontend test does. The end-to-end conversion is covered by the CLI suite, which shares the same helper andbuildPsychDSDataFiles.)E2E verification
Ran the CLI against the real OSF sample copied verbatim (still wrapped) as
subj-1_data.json: "1 files read" (previously skipped), 9 trials → main CSV + 4 sidecars,data/raw/subj-1_data.jsonbyte-identical to the original, and Psych-DS validation passed (warnings only).Follow-up note
When the JSON-Lines ingestion work lands, its parsing path should reuse
unwrapTrialsso wrapper support is consistent across JSON and JSONL inputs (flagged with aTODOin the helper).🤖 Generated with Claude Code