Jolt 将逗号分隔数组转换为数组

问题描述 投票:0回答:1

我是 JOLT 新手。 这就是我想要实现的转变:

输入的json是一个以逗号分隔的对象。输入的 json 如下所示:

{
  "Ids": {
    "IND": {
      "IND-ADR": {
        "id": "ind-adr-id",
        "key": "ind1"
      },
      "IND-PAN": {
        "id": "ind-pan-id",
        "key": "ind2"
      }
    },
    "USA": {
      "USA-SSN": {
        "id": "usa-ssn-id",
        "key": "usa1"
      }
    }
  }
}

预期输出看起来像列表中的 3 个独立项目。预期输出:

{
  "Ids": [
    {
      "country": "IND",
      "IdType": "IND-ADR",
      "id": "ind-adr-id",
      "key": "ind1"
    },
    {
      "country": "IND",
      "IdType": "IND-PAN",
      "id": "ind-pan-id",
      "key": "ind2"
    },
    {
      "country": "USA",
      "IdType": "USA-SSN",
      "id": "usa-ssn-id",
      "key": "usa1"
    }
  ]
}

我的输出:

{
  "country" : [ "IND", "USA" ],
  "IdType" : [ "IND-ADR", "IND-PAN", "USA-SSN" ],
  "id" : [ "ind-adr-id", "ind-pan-id", "usa-ssn-id" ],
  "Key" : [ "ind1", "ind2", "usa1" ]
}

我的震动规格:

[
  {
    "operation": "shift",
    "spec": {
      "Ids": {
        "*": {
          "$": "country",
          "*": {
            "$": "IdType",
            "*": "&"
          }
        }
      }
    }
  }
]

我做错了什么?如何使用震动规格转换获得预期输出。我正在使用 Jolt 演示网站来玩玩,非常感谢对此的一些帮助。

提前致谢。

json transformation jolt
1个回答
0
投票

您的规格非常好,但缺少以下内容:

  • 通过不同级别的&1&2&3等标识符进行分隔
  • 从最深层次调用每一对,例如。您也可以使用 $1 表示法将国家/地区添加到最内层

所以下面的转换将解决这个问题

[
  { // separation of objects
    "operation": "shift",
    "spec": {
      "Ids": {
        "*": {
          "*": {
            "$1": "&2_&1.&3.country", // &3 needed to keep the literal "Ids" for the next spec
            "$": "&2_&1.&3.IdType",
            "*": "&2_&1.&3.&"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": "&1[#3].&" //&1 represents going one level and copying the key, eg. "Ids"
        }
      }
    }
  }
]

网站上的 演示 http://jolt-demo.appspot.com/ 是:

© www.soinside.com 2019 - 2024. All rights reserved.