[英]Implement a envelope pattern in Marklogic using Xquery?
I want to implement envelope pattern in my json data. 我想在我的json数据中实现信封模式。 My data looks like this 我的数据看起来像这样
{
"id": "1",
"first_name": "Shawn",
"last_name": "Grant",
"email": "sgrant0@51.la",
"country": "Liberia",
"ip_address": "37.194.161.124"
}
I want to implement envelope pattern like this 我想实现这样的信封图案
{
"envelope": {
"Code" : "2023"
},
"source": {
"id": "1",
"first_name": "Shawn",
"last_name": "Grant",
"email": "sgrant0@51.la",
"country": "Liberia",
"ip_address": "37.194.161.124"
}
}
I tried the below xquery code but it didnt went well 我尝试了下面的xquery代码,但操作不顺利
xquery version "1.0-ml";
let $doc := fn:collection("transform")
for $i in $doc
let $object := json:object() --> I am struck here because i dont know how to create a envelope header
let $source := $i
return $source
Any help is appreciated 任何帮助表示赞赏
Thanks 谢谢
The way JSON nodes map to the XQuery data model in MarkLogic aren't always intuitive if you're used to XML. 如果您习惯使用XML,JSON节点映射到MarkLogic中的XQuery数据模型的方式并不总是很直观。 Generally, working directly with object nodes makes it a little less confusing: 通常,直接使用对象节点可以减少混乱:
for $doc in fn:collection("transform")
let $source := $doc/object-node()
let $envelope :=
object-node {
"Code" : "2023"
}
return object-node {
"envelope" : $envelope,
"source" : $source
}
I like the approach of using json node constructors as described by wst, but sometimes you need to add properties dynamically, which can be complex with those constructors. 我喜欢wst所描述的使用json节点构造函数的方法,但有时您需要动态添加属性,这些属性对于那些构造函数可能很复杂。 You can also use json:object()
, which is a specialization of map:map()
, or map:map()
directly, and convert that to json nodes using xdmp:to-json
: 您还可以使用json:object()
,它是map:map()
或map:map()
,并使用xdmp:to-json
将其转换为json节点:
for $doc in fn:collection('transform')
return xdmp:to-json(map:new((
map:entry("envelope", map:new((
map:entry("Code", 2023)
))),
map:entry("source", $doc)
)))
HTH! HTH!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.