英文:
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/ 上的 演示 是
编辑:您可以根据新的情况使用以下规范:
[
  {
    "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
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].&"
      }
    }
  }
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论