【问题标题】:JOLT JSON transform values from one-to-many to one-to-oneJOLT JSON 将值从一对多转换为一对一
【发布时间】:2022-11-29 22:52:54
【问题描述】:

我正在尝试使用 JOLT 将一个键映射到数组中的每个值到一个新数组。 有人可以帮我解决这个问题吗:

我的JSON:

[
  {
    "person_id": "1",
    "resources": ["asd", "zxc"]
  },
  {
    "person_id": "2",
    "resources": ["ghj", "asd"]
  }
]

我预期的 JSON:

[
  {
    "person_id": "1",
    "resource": "asd"
  },
  {
    "person_id": "1",
    "resource": "zxc"
  },
  {
    "person_id": "2",
    "resource": "ghj"
  },
  {
    "person_id": "2",
    "resource": "asd"
  }
]

我试过这个 Jolt 规范

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "resources": {
          "*": {
            "@(2,person_id)": "[&].person_id",
            "@": "[&].resource"
          }
        }
      }
    }
  }
]

但不幸的是,它总是将同一索引处的所有值映射到 1 个数组。

【问题讨论】:

    标签: json jolt


    【解决方案1】:

    您可以连续使用两个转移通过遍历转换规范resources第一个中的数组使得

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*s": { // this tecnique stands for extracting by the replacement of the asterisk through use of &(2,1) below
              "*": {
                "@(2,person_id)": "[&3].&1.person_id", // to separate by indexes of the array to generate three level for three independent objects
                "@": "[&3].&1.&(2,1)"
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": ""
          }
        }
      }
    ]
    

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

    【讨论】:

      【解决方案2】:

      你可以使用这个颠簸规范:

      [
        {
          "operation": "shift",
          "spec": {
            "*": {
              "*s": {
                "*": {
                  "@(2,person_id)": "@(1).person_id",
                  "@": "@(1).&(2,1)"
                }
              }
            }
          }
        },
        {
          "operation": "shift",
          "spec": {
            "*": ""
          }
        }
      ]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多