使用Jolt将关键变量Case转换为驼峰命名法

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

Converting the key variable Case to Camel Case using Jolt

问题

以下是您提供的内容的翻译:

输入的 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"
  }
]
英文:

I am getting data from the database sources in the following format with capital keys.
I want to convert keys in camel case using the JOLT transformation.

json which I may be getting does not contains fixed key value pairs. Key can vary case-to-case basis

input 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"
  }
]

Expected output

[
  {
    "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"
  }
]

答案1

得分: 2

你可以使用以下规范

[// 为了给将不会消失的属性的空值指定默认值“null”而准备下一个规范
  {
    "operation": "default",
    "spec": {
      "*": {
        "*": "null"
      }
    }
  },
  {// 交换键值对
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "$": "&2.&1.@(0)"
        }
      }
    }
  },
  {// 通过下划线分割值,以将它们转换为每个属性的独立数组
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "*": "=split('_',@(1,&))"
        }
      }
    }
  },
  {// 将所有值转换为小写字母
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": "=toLower"
          }
        }
      }
    }
  },
  {// 列举数组的每个组件
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": "&3.&2.&1.&"
          }
        }
      }
    }
  },
  {// 逐字拆分这些组件
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": "=split('',@(1,&))"
          }
        }
      }
    }
  },
  {// 列举数组的每个组件
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": {
              "*": "&4.&3.&2.&1.&"
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {// 仅通过使用第零索引将派生部分的第一个字母转换为大写字母
        "*": {
          "*": {
            "*": {
              "0": "=toUpper"
            }
          }
        }
      }
    }
  },
  {// 重新排列元素,准备合并这些部分
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": {
              "*": "&4.&3.&2"
            }
          }
        }
      }
    }
  },
  {// 合并这些部分以获得新值
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "*": "=join('',@(1,&))"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {// 使值成为键,而键成为值
      "*": {
        "*": {
          "*": {
            "$": "[&3].@(0)"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {// 再次获取真实的空值
        "*": {
          "null": "[&2].&1",
          "*": {
            "@1": "[&3].&2"
          }
        }
      }
    }
  }
]

两个直接的障碍:

  1. null 值在键值交换过程中消失。使用第一个和最后一个规范来处理这个问题。
  2. 相同的值会导致在应用键值交换后无法分开键。在规范中增加了额外的深度层级以处理这个问题。
英文:

You can use the following spec

[// 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"
          }
        }
      }
    }
  }
]

There are two immediate handicaps :

  1. Disappearing null values during key-value exchange. Used the first and the last specs to handle this issue.

  2. Identical values which would prevent separation of keys after applied key-value exchange. An extra level of deepness added to specs in order to handle this.

huangapple
  • 本文由 发表于 2023年2月8日 14:48:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382225.html
匿名

发表评论

匿名网友

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

确定