【问题标题】:Jolt grouping together颠簸组合在一起
【发布时间】:2017-06-21 11:25:32
【问题描述】:

我有这个 JSON 输入,我很难将事物组合在一起:

[   
    {
        "PK": "123",
        "SURNAME": "CHEN",
        "SEX": "F",
        "DATE_OF_BIRTH": "1962-08-29 00:00:00.0",
        "PHONE_TYPE": "05",
        "PHONE_NO": "12312312",
        "OPERATION": "INSERT",
    }, {
        "PK": "123",
        "SURNAME": "CHEN",
        "SEX": "F",
        "DATE_OF_BIRTH": "1962-08-29 00:00:00.0",
        "PHONE_TYPE": "04",
        "PHONE_NO": "78787878",
        "OPERATION": "UPDATE"
    },{
        "PK": "456",
        "SURNAME": "DEV",
        "SEX": "M",
        "DATE_OF_BIRTH": "1953-06-06 00:00:00.0",
        "PHONE_TYPE": "05",
        "PHONE_NO": "34343434",
        "OPERATION": "INSERT"
    }, {
        "CLIENT_ID": "456",
        "SURNAME": "DEV",
        "SEX": "M",
        "DATE_OF_BIRTH": "1953-06-06 00:00:00.0",
        "PHONE_TYPE": "02",
        "PHONE_NO": "56565656",
        "OPERATION": "DELETE",
    }
]

这是预期的输出:

{
    "Customers": [{
            "MatchingProfile": {
                "CustomerNumber": "", // leave blank
                "DBType": "Oracle",
                "DBKey": "123",
                "LastName": "CHEN",
                "Gender": "Female",
                "Birthdate": "1962-08-29",
            },
            "Contacts": [{
                    "ContactType": "Fax",
                    "CountryCode": "", // leave blank
                    "Phone_Number": "12312312",
                    "Status": "Active"
                }, {
                    "ContactType": "Mobile",
                    "CountryCode": "", // leave blank
                    "PhoneNumber": "78787878",
                    "Status": "Active"
                }
            ]
        },{
            "MatchingProfile": {
                "CustomerNumber": "", // leave blank
                "DBType": "Oracle",
                "DBKey": "456",
                "LastName": "DEV",
                "Gender": "Male",
                "Birthdate": "1953-06-06",
            },
            "Contacts": [{
                    "ContactType": "Fax",
                    "CountryCode": "", // leave blank
                    "PhoneNumber": "34343434",
                    "Status": "Active"
                }, {
                    "ContactType": "Office",
                    "CountryCode": "", // leave blank
                    "PhoneNumber": "56565656",
                    "Status": "Inactive"
                }
            ]
        }
    ]
}

输入的 SEX 是“M”、“F”,以及一些其他编码值。输出性别的对应值为“男性”、“女性”,否则为“”(空白)。 (不要指责我有性别偏见,我知道,这是项目要求,好吗?不是我的要求)

输入“INSERT”和“UPDATE”的操作将是相应的状态:“活动”;对于“删除”,它将是状态:“非活动”。

加上 Birthdate 输出被截断,相当于 DATE_OF_BIRTH 减去时间。

PHONE_TYPE 如下:02 - “Office”,04 - “Mobile”,05 - “Fax”(我故意省略了其他)。

是否可以在 Jolt 中对此进行映射?你能展示一个规格吗?我是 Jolt 的新手,我有点困惑。这比 Excel Pivot 难 10 倍。

【问题讨论】:

    标签: json jolt


    【解决方案1】:

    这几乎与 OOTB Jolt 所能达到的一样接近。注意 Jolt 用于更改数据结构,而不是对诸如“PHONE_TYPE”之类的内容进行自定义数据映射:“04”表示“传真”。

    转换后的输出

    {
      "Customers" : [ {
        "MatchingProfile" : {
          "DBKey" : "123",
          "Gender" : "F",
          "LastName" : "CHEN",
          "Birthdate" : "1962-08-29 00:00:00.0",
          "Contacts" : [ {
            "ContactType" : "05",
            "Phone_Number" : "12312312",
            "Status" : "INSERT"
          }, {
            "ContactType" : "04",
            "Phone_Number" : "78787878",
            "Status" : "UPDATE"
          } ]
        }
      }, {
        "MatchingProfile" : {
          "DBKey" : "456",
          "Gender" : "M",
          "LastName" : "DEV",
          "Birthdate" : "1953-06-06 00:00:00.0",
          "Contacts" : [ {
            "ContactType" : "05",
            "Phone_Number" : "34343434",
            "Status" : "INSERT"
          }, {
            "ContactType" : "02",
            "Phone_Number" : "56565656",
            "Status" : "DELETE"
          } ]
        }
      } ]
    }
    

    震动规格

    [
      // first pivot by the value of SURNAME
      {
        "operation": "shift",
        "spec": {
          "*": { // for each item in the array
            "SURNAME": { // match SURNAME
              "*": { // match any value of SURNAME
                "@2": "&[]" // copy the whole record from 2 levels up to the SURNAME as an array, so we know that in the next step it is always an array
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": { // match CHEN or DEV
            "0": {
              // only pull pk, sex, dob from the first entry of the SURNAME array so as to not duplicate output
              "PK": "Customers[#3].MatchingProfile.DBKey",
              "SEX": "Customers[#3].MatchingProfile.Gender",
              "SURNAME": "Customers[#3].MatchingProfile.LastName",
              "DATE_OF_BIRTH": "Customers[#3].MatchingProfile.Birthdate",
    
              // this does mean that the PHONE_TYPE has to be dealt with twice
              // once for the zeroth item, and then once again for the rest
              "PHONE_TYPE": "Customers[#3].MatchingProfile.Contacts[0].ContactType",
              "PHONE_NO": "Customers[#3].MatchingProfile.Contacts[0].Phone_Number",
              "OPERATION": "Customers[#3].MatchingProfile.Contacts[0].Status"
            },
            "*": {
              // handle PHONE_TYPE and friends for the other records
              "PHONE_TYPE": "Customers[#3].MatchingProfile.Contacts[&1].ContactType",
              "PHONE_NO": "Customers[#3].MatchingProfile.Contacts[&1].Phone_Number",
              "OPERATION": "Customers[#3].MatchingProfile.Contacts[&1].Status"
            }
          }
        }
      }
    ]
    

    如果您发现 Jolt 对枢轴和结构更改有价值,那么您最好的选择是“修复”您的输入数据数组,也就是将“PHONE_TYPE”:“04”映射到“Fax”,修剪 00:00 :00 从生日开始,然后使用 Jolt 制作嵌套的“Customers[].MatchingProfile.Contacts[]”结构。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多