简体   繁体   English

如何在MarkLogic中以编程方式在XQuery中创建JSON?

[英]How do I programmatically create JSON in XQuery in MarkLogic?

I need to build up a JSON node in XQuery in MarkLogic. 我需要在MarkLogic中的XQuery中构建一个JSON节点。 I know that I can use xdmp:unquote() to parse from a string into a node() . 我知道我可以使用xdmp:unquote()从字符串解析成node() However, I'd like to build the JSON programmatically, without ugly string concatenation. 但是,我想以编程方式构建JSON,而不需要丑陋的字符串连接。 I can use computed element constructors to build XML nodes in XQuery. 我可以使用计算元素构造函数在XQuery中构建XML节点。 Is there something similar for JSON nodes? JSON节点有类似的东西吗?

JSON is implemented in MarkLogic as an extension to the XML data model. JSON在MarkLogic中实现为XML数据模型的扩展。 MarkLogic 8 introduces object-node , array-node , number-node , boolean-node , and null-node tests and constructors. MarkLogic 8引入了object-nodearray-nodenumber-nodeboolean-nodenull-node测试和构造函数。 Thus, in XQuery you can build JSON with computed constructors, just like you would with XML. 因此,在XQuery中,您可以使用计算构造函数构建JSON,就像使用XML一样。 For example, 例如,

object-node { 
  "key" || fn:string(xdmp:random(100)): array-node { 1, 2, 3 }, 
  "another": object-node { "child":  text {'asdf'} },
  "lastButNotLeast": boolean-node { fn:true() }
}

will create the JSON, 将创建JSON,

{
  "key47": [1, 2, 3],
  "another": {
    "child": "asdf"
  },
  "lastButNotLeast": true
}

Aside: In JavaScript you can build JSON-like structures as JavaScript objects using JavaScript syntax. 另外:在JavaScript中,您可以使用JavaScript语法将类似JSON的结构构建为JavaScript对象。 You can convert a JavaScript object into a JSON node using xdmp.toJSON() . 您可以使用xdmp.toJSON()将JavaScript对象转换为JSON节点。 Most builtin functions that require a JSON node, however, will do this conversion automatically, such as xdmp.documentInsert() . 但是,大多数需要JSON节点的内置函数都会自动执行此转换,例如xdmp.documentInsert()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM