将内存节点转换为JSON



我需要将der内存节点转换为JSON。预期输出:

{
"citations": [
{
"cited": "classes",
"procceding": [
"applied",
"considered",
"followed"
]
},
{
"cited": "toCase",
"procceding": [
"Ty Corp Ltd v Nu Inc",
"PY Arbitrage v Bank of WN"
]
}
]
}

我的XQuery:

let $nodes :=
<cases>
<citations>
<classes>
<text>applied</text>
<text>considered</text>
<text>followed</text>
</classes>
<toCase>
<text>Ty Corp Ltd v Nu Inc</text>
<text>PY Arbitrage v Bank of WN</text>
</toCase>
</citations>
</cases>
return     
let $map := map:map()
let $p :=
for $node in $nodes/citations/node()
let $nodeName := local-name($node)
let $c :=
map:put($map, "citations", map:new(map:entry('cited', map:new(map:entry($nodeName, (for $value in $node/node() return $value/fn:string(.)) ))) )  )
return $c
return xdmp:to-json($map)

$nodes转换错误:

{引用文献:{引用案例:[Ty Corp Ltd诉Nu Inc案,PY Arbitrage诉WN银行案]}

如何获得预期的JSON?

-(您可以使用json:object((在内存中构建json模型(相当于XML模式(并保留object顺序。我重构并精简了我的一个xml到json模块,如下所示。它应该能满足你的需求。

declare function local:marshal-json(
$master-node as node()
) as item()
{
let $master-object := json:object()
let $_ :=
for $child-node in $master-node/node()
let $child-name := local-name($child-node)
return
map:put($master-object, $child-name,
for $floor in $child-node/node() return local:floor-object($floor)
)      
return $master-object        
};
declare function local:floor-object(
$child-node as node()  
) as item()
{
let $floor-object := json:object()
let $_ := map:put($floor-object, "cited", local-name($child-node))
let $floor-values := json:array()
let $_ :=
for $gc in $child-node/node()
return json:array-push($floor-values, $gc/fn:string(.))
let $_ := map:put($floor-object, "procceding", $floor-values)  
return
$floor-object  
};
let $nodes :=
<cases>
<citations>
<classes>
<text>applied</text>
<text>considered</text>
<text>followed</text>
</classes>
<toCase>
<text>Ty Corp Ltd v Nu Inc</text>
<text>PY Arbitrage v Bank of WN</text>
</toCase>
</citations>
</cases>
return 
local:marshal-json($nodes)

--)我不能反对你的设计。但我赞同这种精神。您应该能够使用类似的逻辑将XML转换为JSON(参数不是硬编码的JSON名称(。

示例:转换

<investment>
<entity>
<name>XYZ</name>
</entity>
<bogus>encrypted</bogus>
<property>
<Canadian>
<propertyType>eligible dividend of CPC</propertyType>
<notionalAmount>10000</notionalAmount>
</Canadian>
<foreign>
<propertyType>dividend of trust</propertyType>
<notionalAmount>7000</notionalAmount>
<foreignWithholding>660</foreignWithholding>
</foreign>
</property>
<capital>
<portfolio>
<name>HF25</name>
<inadequateConsiderationFMV>10000</inadequateConsiderationFMV>
<transferredAmount>15000</transferredAmount>
<gainLossOnDisposition>11000</gainLossOnDisposition>
</portfolio>
<portfolio>
<name>UL1</name>
<superficialLoss>17000</superficialLoss>
<reacquired>5000</reacquired>
<gainLossOnDisposition>-2000</gainLossOnDisposition>
</portfolio>
<security-CUSIP1>
<gainLossOnDisposition>-3600</gainLossOnDisposition>
</security-CUSIP1>
</capital>
</investment>

到JSON:

{
"entity": {
"name": "XYZ"
},
"bogus": null,
"property": {
"Canadian": {
"propertyType": "eligible dividend of CPC",
"notionalAmount": 10000
},
"foreign": {
"propertyType": "dividend of trust",
"notionalAmount": 7000,
"foreignWithholding": 660
}
},
"capital": {
"portfolio": [
{
"name": "HF25",
"inadequateConsiderationFMV": 10000,
"transferredAmount": 15000,
"gainLossOnDisposition": 11000
},
{
"name": "UL1",
"superficialLoss": 17000,
"reacquired": 5000,
"gainLossOnDisposition": -2000
}
], 
"security-CUSIP1": {
"gainLossOnDisposition": -3600
}
}
}

我使用更具杠杆作用的低级API:它根据指令将XML字符串/文本转换为JSONstringnumber;它符合自定义的JSON模型、对象和数组结构;它遵循金融领域的XML/JONS命名约定。

--)也就是说,这种编程模型是内存密集型的。如果XSLT3在Java的流模型中解析和转换JSON,它可能会更节省内存。我从来没有像现在这样使用XSLT3 JSON解析器,也不能评论它的功效。

let $nodes :=
<cases>
<citations>
<classes>
<text>applied</text>
<text>considered</text>
<text>followed</text>
</classes>
<toCase>
<text>Ty Corp Ltd v Nu Inc</text>
<text>PY Arbitrage v Bank of WN</text>
</toCase>
</citations>
</cases>
return     
let $citations := 
for $node in $nodes/citations/node()
let $nodeName := local-name($node)
return 
map:new(( 
map:entry('cited', $nodeName), 
map:entry("procceding", for $value in $node/node() return $value/fn:string(.) )
))   
return 
map:entry("citations", $citations) => xdmp:to-json() 

最新更新