问题描述
如何在XQUERY Marklogic中获取json文档的每个字段?
let $doc :=
{
"field1" :'t',"field2" : 'th',"filed4": 'the'
}
return
$doc//??,{
"New Filed" : "Added"
}
那么我们如何获得如下所示的输出?
{ "field1" :'t',"filed4": 'the',"New Filed" : "Added"}
解决方法
一种方法:使用xdmp:from-json()
将不可变的JSON节点转换为可变映射,然后设置字段:
return xdmp:from-json($doc) => map:with("NewField","Added")
有关更多详细信息,请参见:https://docs.marklogic.com/xdmp:from-json
希望有帮助,
, JSON对象实际上只是一个专用映射。因此,您可以使用地图运算符,例如+
union operator:
let $doc := object-node
{
"field1" :'t',"field2" : 'th',"filed4": 'the'
}
return
$doc + map:entry("NewField","added")