[英]How to transform list with in a list using jolt library
我正在尝试使用 Jolt 库将 json 转换为另一个,但没有获得所需的 output。
这是我的输入文件。
{ "maxResults": 150, "total": 89, "issues": [ { "key": "1", "fields": { "fixVersions": [ { "self": "FIX1-01", "id": "11" }, { "self": "FIX1-02", "id": "12" } ] } }, { "key": "2", "fields": { "fixVersions": [ { "self": "FIX2-01", "id": "21" } ] } }, { "key": "3", "fields": { "fixVersions": [] } } ] }
And this is the spec file I am using for transformation.
<pre>
[
{
"operation": "shift",
"spec": {
"issues": {
"*": {
"key": "[&1].id",
"fields": {
"fixVersions": {
"*": {
"self": "[&1].fixVersion.name"
}
}
}
}
}
}
}
]
我得到这个 output
[ { "id" : "1", "fixVersion" : { "name" : [ "FIX1-01", "FIX2-01" ] } }, { "fixVersion" : { "name" : "FIX1-02" }, "id" : "2" }, { "id" : "3" } ]
这是不对的。 它所做的是获取每个问题的第一个 self 字段值,并将其作为数组填充到 output 的第一个 fixVersions 中,并获取第二个 self 字段值并将其填充到第二个 fixVersions 中。 我想要做的是将结构保持在输入中,只需像这样更改 self 字段名称。
[ { "id" : "1", "fixVersion" : [ { "name" : "FIX1-01" }, { "name" : "FIX1-02" } ] }, { "fixVersion" : [ { "name" : "FIX2-01" } ], "id" : "2" }, { "id" : "3" } ]
我究竟做错了什么?
当您使用 XSLT 标签和 Java 标签时,您可以使用 XSLT 和/或 XQuery 3 与 Saxon 9 并使用例如
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
declare option output:method 'text';
declare variable $json as xs:string external := '{
"maxResults": 150,
"total": 89,
"issues": [
{
"key": "1",
"fields": {
"fixVersions": [
{
"self": "FIX1-01",
"id": "11"
},
{
"self": "FIX1-02",
"id": "12"
}
]
}
},
{
"key": "2",
"fields": {
"fixVersions": [
{
"self": "FIX2-01",
"id": "21"
}
]
}
},
{
"key": "3",
"fields": {
"fixVersions": []
}
}
]
}';
transform(map {
'source-node' : json-to-xml($json),
'stylesheet-node' : <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="*:string[@key = 'self']">
<xsl:copy>
<xsl:attribute name="key">name</xsl:attribute>
<xsl:value-of select="."/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
})?output => xml-to-json(map { 'indent' : true() })
看看这个规范是否是你正在寻找的:
[
{
"operation": "shift",
"spec": {
"issues": {
"*": {
"key": "[&1].id",
"fields": {
"fixVersions": {
"*": {
"self": "[&4].fixVersion[&1].name"
}
}
}
}
}
}
}
]
Output 对您的输入使用它会导致:
[ {
"id" : "1",
"fixVersion" : [ {
"name" : "FIX1-01"
}, {
"name" : "FIX1-02"
} ]
}, {
"id" : "2",
"fixVersion" : [ {
"name" : "FIX2-01"
} ]
}, {
"id" : "3"
} ]
请注意您用于重建 fixVersion 的索引的更改(还需要考虑问题索引)列表,您错过了这个:
[&4].fixVersion[&1].name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.