Search code examples
jsonxquerymarklogicmarklogic-8

Updating JSON in XQuery and Marklogic 8


I've got a simple enough JSON document stored in Marklogic:

{ "title": "mytitle", "edition": "1" }

Is there a way I can add a new field using Xquery functions so my document and object end up looking is this?

{ "title": "mytitle", "edition": "1", "date": "2016-01-01" }

There apparently is support for JSON manipulation via xdmp:node-replace, xdmp:insert-child-after, etc found in the documentation here: https://docs.marklogic.com/guide/app-dev/json#id_60123

I have had 0 luck with the above. Any suggestions?


Solution

  • When you only need to insert a property, you first have to wrap the property/value in an object-node, then select the property child:

    xdmp:node-insert-child(
      doc('/mydoc.json')/node(),
      object-node { "date": "2016-01-01" }/date)