Skip to content

Loading JSON into messageCache

When a JSON source's top-level fields need to be accessible as individual keys by later mapping / condition / destination nodes in the same channel, mirror them into messageCache at the top of the pipeline. Subsequent nodes then read the values by name without re-parsing the JSON body.

var body = JSON.parse(source.toString());

for (var key in body) {
    if (body.hasOwnProperty(key)) {
        messageCache.setValue(key, '' + body[key]);
    }
}

Each value is coerced to a string with '' + value before being written. messageCache.setValue stores strings, so passing a number or boolean directly ends up as the object's string form anyway; the explicit coercion also flattens null/undefined to the literals "null"/"undefined" (guard against that if the JSON has optional fields you would rather skip).

Downstream nodes read the values with:

var mrn = messageCache.getValue('mrn');

Skipping empty and nested values

For a JSON source where some top-level values are objects or arrays rather than scalars, the coercion above turns them into [object Object] / a comma-joined list, usually not what you want. Filter for scalar values before writing:

var body = JSON.parse(source.toString());

for (var key in body) {
    if (!body.hasOwnProperty(key)) continue;

    var value = body[key];
    if (value === null || value === undefined) continue;   // skip missing
    if (typeof value === 'object') continue;               // skip nested objects/arrays

    messageCache.setValue(key, '' + value);
}

For nested structures that need to survive the cache round-trip, JSON-stringify each nested value and re-parse on the way out:

messageCache.setValue('patient', JSON.stringify(body.patient));
// …later, in a downstream node:
var patient = JSON.parse(messageCache.getValue('patient'));