Skip to main content

Using MarkLogic Content Pump (mlcp)

Example Implementation

The following example adds a property named “NEWPROP” to incoming JSON documents and leaves non-JSON documents unmodified. The property value is specified on the mlcp command line, using the -transform_param option.

// Add a property named "NEWPROP" to any JSON input document.
// Otherwise, input passes through unchanged.
function addProp(content, context)
{
  const propVal = (context.transform_param == undefined)
                 ? "UNDEFINED" : context.transform_param;
  if (xdmp.nodeKind(content.value) == 'document' &&
      content.value.documentFormat == 'JSON') {
    // Convert input to mutable object and add new property
    const newDoc = content.value.toObject();
    newDoc.NEWPROP = propVal;
    // Convert result back into a document
    content.value = xdmp.unquote(xdmp.quote(newDoc));
  }
  return content;
};
exports.addProp = addProp;