Skip to content
Astera Technology

Flatten object leaves into path-array answers

Keep scalar arrays whole and distinguish empty objects from absent branches.

Download complete case notes and answer keys ↓

Source contract and expected records

This is an original, fully synthetic mapping example. Inputs are plain, acyclic JSON-shaped records with the fields shown; the canonical shape and rejection codes belong to this example. Adapt the contract before using a different source.

For this acyclic JSON-shaped payload, recursively visit object fields. Emit scalar leaves with a path array. Arrays of scalars remain one whole answer; object-valued arrays are rejected. Emit an empty object as an explicit {} answer rather than omitting it. Path arrays preserve field-name boundaries without joining them into text.

Editable synthetic mapping cases
CaseInput record (JSON)Expected output (JSON)
Capture nested answer leaves{"payload":{"profile":{"name":"Ada","tags":["new","trial"]},"optIn":false}}{"ok":true,"value":{"answers":[{"path":["profile","name"],"value":"Ada"},{"path":["profile","tags"],"value":["new","trial"]},{"path":["optIn"],"value":false}]}}
Reject object-valued array{"payload":{"tags":[{"code":"new"}]}}{"ok":false,"error":"LEAF_ARRAY"}
Preserve empty object{"payload":{"profile":{}}}{"ok":true,"value":{"answers":[{"path":["profile"],"value":{}}]}}
Download editable CSV ↓

Run the local transformation

Run the complete example as a JavaScript module in Node.js. It contains the pure mapping function, all three inputs and literal expected outputs; assertions stop on a mismatch. A rejected case returns ok=false with an error code, rather than a partial canonical record.

Complete function and executable cases
import assert from 'node:assert/strict';

function leafAnswers(i){
  const fail=error=>({ok:false,error});if(!i||!Object.hasOwn(i,'payload'))return fail('LEAF_PAYLOAD');
  const scalar=v=>v===null||typeof v==='string'||typeof v==='boolean'||(typeof v==='number'&&Number.isFinite(v));
  const answers=[];let error;
  const visit=(value,path)=>{
    if(scalar(value)){answers.push({path,value});return;}
    if(Array.isArray(value)){if(!value.every(scalar)){error='LEAF_ARRAY';return;}answers.push({path,value:[...value]});return;}
    if(value&&typeof value==='object'){
      const entries=Object.entries(value);if(!entries.length){answers.push({path,value:{}});return;}
      for(const [key,v]of entries)visit(v,[...path,key]);return;
    }
    error='LEAF_VALUE';
  };
  visit(i.payload,[]);return error?fail(error):{ok:true,value:{answers}};
}

// Original synthetic cases; expected outputs are literal records.
const cases = [
  {
    "label": "Capture nested answer leaves",
    "input": {
      "payload": {
        "profile": {
          "name": "Ada",
          "tags": [
            "new",
            "trial"
          ]
        },
        "optIn": false
      }
    },
    "expected": {
      "ok": true,
      "value": {
        "answers": [
          {
            "path": [
              "profile",
              "name"
            ],
            "value": "Ada"
          },
          {
            "path": [
              "profile",
              "tags"
            ],
            "value": [
              "new",
              "trial"
            ]
          },
          {
            "path": [
              "optIn"
            ],
            "value": false
          }
        ]
      }
    }
  },
  {
    "label": "Reject object-valued array",
    "input": {
      "payload": {
        "tags": [
          {
            "code": "new"
          }
        ]
      }
    },
    "expected": {
      "ok": false,
      "error": "LEAF_ARRAY"
    }
  },
  {
    "label": "Preserve empty object",
    "input": {
      "payload": {
        "profile": {}
      }
    },
    "expected": {
      "ok": true,
      "value": {
        "answers": [
          {
            "path": [
              "profile"
            ],
            "value": {}
          }
        ]
      }
    }
  }
];
for (const sample of cases) {
  const actual = leafAnswers(sample.input);
  assert.deepEqual(actual, sample.expected);
  console.log(sample.label, JSON.stringify(actual));
}
Download runnable example ↓

Work through the boundary

Does tags=["new","trial"] produce two separate indexed answers?

Show the worked answer

No. It remains one answer at ["profile","tags"] with the full array. optIn=false is another explicit answer, while an empty profile produces an answer value {}.

Related resources