如何使用 xquery 从 json 文件中删除一个元素

how to delete an element from json file using xquery

我在 MarkLogic 中有一个 .json 文件:

{
  "contextProductIdCDM": {
    "variables": {
      "source": "'Inm'", 
      "source_table_id": "'123'", 
      "Referdate": "normalize-space(string-join((J_MCCC, if(J_MCDC eq '') then '000' else J_MCDC),''))"
    }, 
    "$ref": "#/contexts/Closure"
  }, 
  "predcondition": "xyz=1"
}

我想删除前提条件,还想删除部分引用数据,也使用 xquery。谁能帮助实现这一目标?

如果您想删除 precondition 字段并修改 contextProductIdCDM.variables.Referdate 值,您可以使用 XPath 处理那些 JSON 属性并使用 xdmp:node-delete() and xdmp:node-replace() 函数:

xquery version "1.0-ml";
let $doc := fn:doc("/test.json")
return (
  xdmp:node-replace($doc/contextProductIdCDM/variables/Referdate, text{ "000" }), 
  xdmp:node-delete($doc/predcondition)
)

如何在 JavaScript 中完成;转换为普通的旧对象,进行修改,然后用更新后的 JSON 对象替换文档:

declareUpdate();
const doc = cts.doc("/test.json");
let obj = doc.toObject(); // create mutable representation
obj.contextProductIdCDM.variables.Referdate = '000'; //change the Referdate property value
delete obj.predcondition; // delete precondition field
xdmp.nodeReplace(doc, obj); // update the JSON document with the changes