Reputation: 223
I am using Javascript and JSON files. I am trying to create a child node and insert a value into that node
The base setup is:
declareUpdate();
xdmp.documentInsert("/example.json",
{"a":"aa","b":"bb"});
What I would like to see is a child node in either a or b with some data.
declareUpdate(); var doc = cts.doc("/example.json"); var docObj = doc.toObject(); docObj. = "this is a much different value"; xdmp.nodeInsertChild(cts.doc("/example.json").xpath("/a"), docObj);
[javascript] XDMP-CHILDNODEKIND: xdmp.nodeInsertChild(Sequence(xdmp.unpath("fn:doc('/foo.json')/text('foo')")), Text("this is a child node value")) -- text nodes cannot have text node children
Upvotes: 0
Views: 262
Reputation: 1368
The error suggests your attempt of adding a JSON property as immediate child of the document node. MarkLogic object node can’t have document node children.
One effective way of updating JSON document node is to update/replace the JavaScript object node with your construct. (Note: You can’t insert a text expression directly into MarkLogic JSON node, insert a named node instead.)
If the desired result is something like
{
"a": {
"a1": "aa",
"a2": "new child node"
},
"b": "bb"
}
, then simply apply a node replace:
declareUpdate();
const doc = cts.doc("/example.json");
const newNode = { "a1":"aa", "a2":"new child node" };
xdmp.nodeReplace(doc.xpath("/a"), newNode);
Upvotes: 2