json 使用Jolt将键变量Case转换为Camel Case

tuwxkamq  于 2023-02-10  发布在  其他
关注(0)|答案(2)|浏览(130)

我正在从数据库源中获取以下格式的大写键数据。我想使用JOLT转换将键转换为驼峰式。
我可能得到的json不包含固定的键值对。Key可以根据具体情况而变化

输入json

[
  {
    "LAST_UPDT_TS": "2018-05-21 07:52:06.0",
    "RTRV_TS": "2023-02-08 06:03:03.932108",
    "DOC_ID": "1-102GJ8CY",
    "PARENT_ASSET_DOC_ID": null,
    "ASSET_STATUS": "Inactive",
    "CAGE_NUM": "SUBZ6G"
  },
  {
    "LAST_UPDT_TS": "2020-09-09 22:28:25.0",
    "RTRV_TS": "2023-02-08 06:03:03.932108",
    "DOC_ID": "1-102MDPE7",
    "PARENT_ASSET_DOC_ID": null,
    "ASSET_STATUS": "Active",
    "CAGE_NUM": "012210"
  }
]

预期产出

[
  {
    "lastUpdtTs": "2018-05-21 07:52:06.0",
    "rtrvTs": "2023-02-08 06:03:03.932108",
    "docId": "1-102GJ8CY",
    "ParentAssetDocId": null,
    "AssetStatus": "Inactive",
    "CageNum": "SUBZ6G"
  },
  {
    "lastUpdtTs": "2020-09-09 22:28:25.0",
    "retrieveTimestamp": "2023-02-08 06:03:03.932108",
    "docId": "1-102MDPE7",
    "ParentAssetDocId": null,
    "AssetStatus": "Active",
    "CageNum": "012210"
  }
]
sgtfey8w

sgtfey8w1#

您可以使用以下规范

[// Preparation for the next spec for assigning default "null" for null values of which the attributes won't vanish
  {
    "operation": "default",
    "spec": {
      "*": {
        "*": "null"
      }
    }
  },
  {// Exchange key-value pairs
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "$": "&2.&1.@(0)"
        }
      }
    }
  },
  {// Split values by underscores in order to convert them to independent arrays for each attribute
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "*": "=split('_',@(1,&))"
        }
      }
    }
  },
  {// Convert all values to lowercase letters
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": "=toLower"
          }
        }
      }
    }
  },
  {// Enumerate each components of the arrays
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": "&3.&2.&1.&"
          }
        }
      }
    }
  },
  {// Split those components letterwise
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": "=split('',@(1,&))"
          }
        }
      }
    }
  },
  {// Enumerate each components of the arrays
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": {
              "*": "&4.&3.&2.&1.&"
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {// Convert only the first letters of the derived pieces to uppercase letters by use of 0th(zeroth) index
        "*": {
          "*": {
            "*": {
              "0": "=toUpper"
            }
          }
        }
      }
    }
  },
  {// Rearrange the elements to prepare to combine the pieces
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": {
              "*": "&4.&3.&2"
            }
          }
        }
      }
    }
  },
  {// Combine the pieces in order to get new values
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "*": "=join('',@(1,&))"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {// Make values keys, while keys to values
      "*": {
        "*": {
          "*": {
            "$": "[&3].@(0)"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {// get back again the real null values
        "*": {
          "null": "[&2].&1",
          "*": {
            "@1": "[&3].&2"
          }
        }
      }
    }
  }
]

有两个直接的障碍:
1.在键-值交换过程中消失null值。使用第一个和最后一个规范来处理这个问题。
1.相同的值会在应用键-值交换后阻止键的分离。为了处理这个问题,在规范中增加了一个额外的深度。

1cosmwyk

1cosmwyk2#

可以使用此等级库:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "LAST_UPDT_TS": "[&1].lastUpdtTs",
        "RTRV_TS": "[&1].rtrvTs",
        "DOC_ID": "[&1].docId",
        "PARENT_ASSET_DOC_ID": "[&1].ParentAssetDocId",
        "ASSET_STATUS": "[&1].AssetStatus",
        "CAGE_NUM": "[&1].CageNum"
      }
    }
  }
]

相关问题