Fan Li
Fan Li

Reputation: 1077

Convert JSON-Patch to MarkLogic JSON patch specification?

The JSON patch specification used by MarkLogic PATCH API is remarkably similar to the JSON-Path standard (RFC 6902) but not exactly the same. For example, to add a node to the following document:

{
  "parent": {
    "child1": "c1-value",
    "child2": "c2-value"
  }
}

The MarkLogic patch:

{
  "insert": {
    "context": "/parent",
    "position": "last-child",
    "content": { "child3": "c3-value" }
  }
}

The JSON-Patch standard:

{
  "op": "add",
  "path": "/parent/child3",
  "value": "c3-value"
}

Is there a way to automatically translate JSON-Patch into MarkLogic Patch? My thought is to leverage libraries such as json-patch-gen to automatically generate JSON-Patch operations and convert them into MarkLogic Patches to update documents in MarkLogic.

Alternatively, is there a JavaScript library available to automatically generate MarkLogic patches by DIFF-ing two JavaScript objects?

Upvotes: 4

Views: 192

Answers (1)

ehennum
ehennum

Reputation: 7335

While the RFC influenced the MarkLogic patch specification, some stipulations of the RFC are a mismatch for MarkLogic.

  • JSON Pointer has different selection semantics than MarkLogic XPaths over JSON such as selecting container array nodes instead of item nodes. (Also, if I recall correctly, JSON Pointer doesn't support the XPath predicates.)

  • Where the patch operations in MarkLogic must be unrelated and applicable in any order, JSON Patch requires a sequential transformation. As noted in the RFC:

    Each operation in the sequence is applied to the target document; the resulting document becomes the target of the next operation.

  • The RFC doesn't support implementing insert or replace operations in user-defined server-side code.

  • The MarkLogic patch specification must be consistent for JSON and XML.

It would be possible to transform a patch expressed in a subset of the syntax of the JSON Patch RFC into a MarkLogic patch specification that uses a subset of the MarkLogic patch capabilities.

That least-common-denominator approach, however, would add to the cost, create an opportunity for bugs, and give up some features of MarkLogic.

Unless the same patch was applied to the content in other data stores, there might not be much benefit to requiring the transformation.

Hoping that's of use,

Upvotes: 5

Related Questions