java 如何使用jolt库转换列表中的列表

frebpwbc  于 2023-02-07  发布在  Java
关注(0)|答案(2)|浏览(120)

我尝试使用Jolt库将JSON转换为另一个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": []
      }
    }
  ]
}

这是我用于转换的规范文件。

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

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

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

我哪里做错了?

kqlmhetl

kqlmhetl1#

查看此规格是否是您正在寻找的:

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

请注意您用于重建fixVersion(还需要考虑问题索引)列表的索引的更改,您错过了以下内容:

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

djp7away2#

由于您已经使用了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() })

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

相关问题