Skip to content
Astera Technology

Build nested records from dotted field paths

Reject leaf/container collisions while preserving explicit null.

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.

The original field-path grammar uses dot-separated lowercase letter/digit segments, starting with a letter; constructor and prototype segments are forbidden. Leaves may be text, Boolean, finite number or null. Create nested objects from the path. Reject when a path needs an object where another path already placed a leaf, in either insertion order.

Editable synthetic mapping cases
CaseInput record (JSON)Expected output (JSON)
Build objects from field paths{"fields":{"person.name":"Ada","address.city":"Demo"}}{"ok":true,"value":{"record":{"person":{"name":"Ada"},"address":{"city":"Demo"}}}}
Reject leaf/container conflict{"fields":{"person":"Ada","person.name":"Bo"}}{"ok":false,"error":"PATH_CONFLICT"}
Preserve explicit null leaf{"fields":{"request.note":null}}{"ok":true,"value":{"record":{"request":{"note":null}}}}
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 flatPaths(i){
  const fail=error=>({ok:false,error});if(!i||!i.fields||typeof i.fields!=='object'||Array.isArray(i.fields))return fail('PATH_FIELDS');
  const record={};const scalar=v=>v===null||typeof v==='string'||typeof v==='boolean'||(typeof v==='number'&&Number.isFinite(v));
  for(const [path,value]of Object.entries(i.fields)){
    const pieces=path.split('.');
    if(!pieces.every(p=>/^[a-z][a-z0-9]*$/.test(p)&&!['constructor','prototype'].includes(p)))return fail('PATH_NAME');
    if(!scalar(value))return fail('PATH_VALUE');let at=record;
    for(let n=0;n<pieces.length;n++){
      const key=pieces[n];
      if(n===pieces.length-1){if(Object.hasOwn(at,key))return fail('PATH_CONFLICT');at[key]=value;}
      else{
        if(Object.hasOwn(at,key)){if(!at[key]||typeof at[key]!=='object'||Array.isArray(at[key]))return fail('PATH_CONFLICT');}
        else at[key]={};at=at[key];
      }
    }
  }
  return {ok:true,value:{record}};
}

// Original synthetic cases; expected outputs are literal records.
const cases = [
  {
    "label": "Build objects from field paths",
    "input": {
      "fields": {
        "person.name": "Ada",
        "address.city": "Demo"
      }
    },
    "expected": {
      "ok": true,
      "value": {
        "record": {
          "person": {
            "name": "Ada"
          },
          "address": {
            "city": "Demo"
          }
        }
      }
    }
  },
  {
    "label": "Reject leaf/container conflict",
    "input": {
      "fields": {
        "person": "Ada",
        "person.name": "Bo"
      }
    },
    "expected": {
      "ok": false,
      "error": "PATH_CONFLICT"
    }
  },
  {
    "label": "Preserve explicit null leaf",
    "input": {
      "fields": {
        "request.note": null
      }
    },
    "expected": {
      "ok": true,
      "value": {
        "record": {
          "request": {
            "note": null
          }
        }
      }
    }
  }
];
for (const sample of cases) {
  const actual = flatPaths(sample.input);
  assert.deepEqual(actual, sample.expected);
  console.log(sample.label, JSON.stringify(actual));
}
Download runnable example ↓

Work through the boundary

Can person="Ada" coexist with person.name="Bo"?

Show the worked answer

No. person cannot simultaneously be a text leaf and an object container; PATH_CONFLICT rejects the input. A separate request.note=null remains a valid explicit leaf.

Related resources

System integration services →