如何使用 jolt 库在列表中转换列表

How to transform list with in a list using jolt library

我正在尝试使用 Jolt 库将 json 转换为另一个,但没有获得所需的输出。

这是我的输入文件。


    {
      "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"
              }
            }
          }
        }
      }
    }
  }
]

我得到了这个输出


    [ {
      "id" : "1",
      "fixVersion" : {
        "name" : [ "FIX1-01", "FIX2-01" ]
      }
    }, {
      "fixVersion" : {
        "name" : "FIX1-02"
      },
      "id" : "2"
    }, {
      "id" : "3"
    } ]

不对。它正在做的是获取每个问题的第一个自字段值并将其作为数组填充到输出的第一个 fixVersions 中,并获取第二个自字段值并将其填充到第二个 fixVersions 中。我想要做的是保持输入中的结构,只需像这样更改自己的字段名称。


    [ {
      "id" : "1",
      "fixVersion" : [
       { 
        "name" : "FIX1-01" 
       },
       { 
        "name" : "FIX1-02" 
       }
      ]
    }, {
      "fixVersion" : [
       { 
        "name" : "FIX2-01" 
       }
      ],
      "id" : "2"
    }, {
      "id" : "3"
    } ]

我做错了什么?

由于您使用了 XSLT 标签和 Java 标签,您可以将 XSLT and/or 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() })

https://xqueryfiddle.liberty-development.net/bdxZ8R

查看此规格是否是您要找的:

[
  {
    "operation": "shift",
    "spec": {
      "issues": {
        "*": {
          "key": "[&1].id",
          "fields": {
            "fixVersions": {
              "*": {
                "self": "[&4].fixVersion[&1].name"
              }
            }
          }
        }
      }
    }
  }
]

根据您的输入使用它的输出将导致:

[ {
  "id" : "1",
  "fixVersion" : [ {
    "name" : "FIX1-01"
  }, {
    "name" : "FIX1-02"
  } ]
}, {
  "id" : "2",
  "fixVersion" : [ {
    "name" : "FIX2-01"
  } ]
}, {
  "id" : "3"
} ]

请注意您用于重建修复版本的索引的更改(需要考虑何时发布索引)列表,您遗漏了这个:

[&4].fixVersion[&1].name