Jolt从数组到一对多的转换

huangapple go评论57阅读模式
英文:

Jolt transformation from array to one-to-many

问题

这是我的输入 JSON,其中包含用户和设备的数组:

{
  "q": [
    {
      "UserId": "3000",
      "SiteId": "website1",
      "CreateDate": "2022-04-01T08:26:21.401Z",
      "DevideId": "ddd1111",
      "TokenId": "abcd1234"
    },
    {
      "UserId": "3000",
      "SiteId": "website1",
      "CreateDate": "2022-04-01T08:26:21.401Z",
      "DevideId": "ddd2222",
      "TokenId": "wxyz1234"
    },
    {
      "UserId": "3002",
      "SiteId": "website1",
      "CreateDate": "2022-04-01T08:26:21.401Z",
      "DevideId": "ddd333",
      "TokenId": "wxyz1234"
    }
  ]
}

输出 JSON 应该是:

[
  {
    "CreateDate": "2022-04-01T08:26:21.401Z",
    "UserId": "3000",
    "devices": [
      {
        "DevideId": "ddd1111",
        "SiteId": "website1",
        "TokenId": "abcd1234"
      },
      {
        "DevideId": "ddd2222",
        "SiteId": "website1",
        "TokenId": "wxyz1234"
      }
    ]
  },
  {
    "CreateDate": "2022-04-01T08:26:21.401Z",
    "UserId": "3002",
    "devices": [
      {
        "DevideId": "ddd333",
        "SiteId": "website1",
        "TokenId": "wxyz1234"
      }
    ]
  }
]

输出的 JSON 表示一个用户对应多个设备的关系。

英文:

This is my input json, which is array of user and devices

{
  "q": [
    {
      "UserId": "3000",
      "SiteId": "website1",
      "CreateDate": "2022-04-01T08:26:21.401Z",
      "DevideId": "ddd1111",
      "TokenId": "abcd1234"
    },
    {
      "UserId": "3000",
      "SiteId": "website1",
      "CreateDate": "2022-04-01T08:26:21.401Z",
      "DevideId": "ddd2222",
      "TokenId": "wxyz1234"
    },
    {
      "UserId": "3002",
      "SiteId": "website1",
      "CreateDate": "2022-04-01T08:26:21.401Z",
      "DevideId": "ddd333",
      "TokenId": "wxyz1234"
    }
  ]
}

output json should be:

[
      {
        "CreateDate": "2022-04-01T08:26:21.401Z",
        "UserId": "3000",
        "devices": [
          {
            "DevideId": "ddd1111",
            "SiteId": "website1",
            "TokenId": "abcd1234"
          },
          {
            "DevideId": "ddd2222",
            "SiteId": "website1",
            "TokenId": "wxyz1234"
          }
        ]
      },
      {
        "CreateDate": "2022-04-01T08:26:21.401Z",
        "UserId": "3002",
        "devices": [
          {
            "DevideId": "ddd333",
            "SiteId": "website1",
            "TokenId": "wxyz1234"
          }
        ]
      }
    ]

And the output JSON (one user, multiple devices) means array of user-device (one to many relation)
Thanks in advance.

答案1

得分: 0

以下是您要翻译的内容:

[
  { // to seperate those two attributes to the outer level
    "operation": "shift",
    "spec": {
      "q": {
        "*": {
          "UserId|CreateDate": "&",
          "*": "devices[#2].&"
        }
      }
    }
  },
  { // pick only one components from each arrays generated from the two attribues
    "operation": "cardinality",
    "spec": {
      "UserId": "ONE",
      "CreateDate": "ONE"
    }
  },
  { // just to sort regarding the array in the result to be displayed at the bottom
    "operation": "sort"
  }
]

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

Jolt从数组到一对多的转换

编辑:您可以根据新的情况使用以下规范:

[
  {
    "operation": "shift",
    "spec": {
      "q": {
        "*": {
          "UserId|CreateDate": "@1,UserId.&",
          "*": "@1,UserId.devices[&1].&"
        }
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "UserId": "ONE",
        "CreateDate": "ONE"
      }
    }
  },
  { // get rid of redundantly generated null values
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=recursivelySquashNulls"
    }
  },
  { // write the attributes and "devices" seperately in order to be able sort attributes at the top
    "operation": "shift",
    "spec": {
      "*": {
        "CreateDate|UserId": "[#2].&",
        "devices": "[#2].&"
      }
    }
  }
]
英文:

You can use the following transformation spec

[
  { // to seperate those two attributes to the outer level
    "operation": "shift",
    "spec": {
      "q": {
        "*": {
          "UserId|CreateDate": "&",
          "*": "devices[#2].&"
        }
      }
    }
  },
  { // pick only one components from each arrays generated from the two attribues
    "operation": "cardinality",
    "spec": {
      "UserId": "ONE",
      "CreateDate": "ONE"
    }
  },
  { // just to sort regarding the array in the result to be displayed at the bottom
    "operation": "sort"
  }
]

the demo on the site http://jolt-demo.appspot.com/ is

Jolt从数组到一对多的转换

Edit : You can use the following spec based on the new case :

[
  {
    "operation": "shift",
    "spec": {
      "q": {
        "*": {
          "UserId|CreateDate": "@1,UserId.&",
          "*": "@1,UserId.devices[&1].&"
        }
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "UserId": "ONE",
        "CreateDate": "ONE"
      }
    }
  },
  { // get rid of redundantly generated null values
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=recursivelySquashNulls"
    }
  },
  { // write the attributes and "devices" seperately in order to be able sort attributes at the top
    "operation": "shift",
    "spec": {
      "*": {
        "CreateDate|UserId": "[#2].&",
        "devices": "[#2].&"
      }
    }
  }
]

huangapple
  • 本文由 发表于 2023年4月17日 20:07:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定