如何使用Jolt将嵌套数据转换为线性数据

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

How to convert nested data into linear data using jolt

问题

如何使用jolt将这个嵌套数据转换为如下所示的线性格式数据?需要为所有嵌套数据创建单独的条目。每条记录应包含以下5个数据:practice_locprac_numtopIdS1S2

我必须编写一个jolt来进行转换,下面是数据的所有可能情况:

数据

第1种情况:

输入

  1. {
  2. "practice_loc": 120,
  3. "prac_num": 234,
  4. "topId": "t1",
  5. "subList": [
  6. {
  7. "S1": "A1",
  8. "S2": "B1"
  9. },
  10. {
  11. "S1": "A2"
  12. }
  13. ]
  14. }

转换后

  1. {
  2. "practice_loc": 120,
  3. "prac_num": 234,
  4. "topId": "t1",
  5. "S1": "A1",
  6. "S2": "B1"
  7. },
  8. {
  9. "practice_loc": 120,
  10. "prac_num": 234,
  11. "topId": "t1",
  12. "S1": "A2"
  13. }

第2种情况可以是:

输入

  1. {
  2. "practice_loc": 987,
  3. "prac_num": 232,
  4. "topId": "artica",
  5. "subList": [
  6. {
  7. "S1": "A5",
  8. "S2": "B7"
  9. }
  10. ]
  11. }

转换后

  1. {
  2. "practice_loc": 987,
  3. "prac_num": 232,
  4. "topId": "artica",
  5. "S1": "A5",
  6. "S2": "B7"
  7. }

第3种情况可以是:

输入

  1. {
  2. "practice_loc": 334,
  3. "prac_num": 233,
  4. "topId": "plumcherry",
  5. "subList": [
  6. {
  7. "S1": "A3"
  8. }
  9. ]
  10. }

转换后

  1. {
  2. "practice_loc": 334,
  3. "prac_num": 233,
  4. "topId": "plumcherry",
  5. "S1": "A3"
  6. }

第4种情况可以是:

输入

  1. {
  2. "practice_loc": 987,
  3. "prac_num": 232,
  4. "topId": "rose",
  5. "subList": [
  6. {}
  7. ]
  8. }

转换后

  1. {
  2. "practice_loc": 987,
  3. "prac_num": 232,
  4. "topId": "rose"
  5. }

我该如何编写一个jolt,可以处理所有这些情况,并返回转换后的数据,无论输入是哪种类型的?

英文:

How can I convert this nested data to linear format data as mentioned below using jolt. Need to create separate entry for all the nested data. every record should have 5 data in it practice_loc,prac_num,topId,S1 and S2.

I have to write one jolt for transforming, data below are all possibilities of data

Data

1st case ;

Input :

  1. {
  2. "practice_loc": 120,
  3. "prac_num": 234,
  4. "topId": "t1",
  5. "subList": [
  6. {
  7. "S1": "A1",
  8. "S2": "B1"
  9. },
  10. {
  11. "S1": "A2"
  12. }
  13. ]
  14. }

After transformation:

  1. {
  2. "practice_loc": 120,
  3. "prac_num": 234,
  4. "topId": "t1",
  5. "S1": "A1",
  6. "S2": "B1"
  7. },
  8. {
  9. "practice_loc": 120,
  10. "prac_num": 234,
  11. "topId": "t1",
  12. "S1": "A2"
  13. }

2nd case can be;

Input:

  1. {
  2. "practice_loc": 987,
  3. "prac_num": 232,
  4. "topId": "artica",
  5. "subList": [
  6. {
  7. "S1": "A5",
  8. "S2": "B7"
  9. }
  10. ]
  11. }

After transformation :

  1. {
  2. "practice_loc": 987,
  3. "prac_num": 232,
  4. "topId": "artica",
  5. "S1": "A5",
  6. "S2": "B7"
  7. }

3rd case can be;

Input :

  1. {
  2. "practice_loc": 334,
  3. "prac_num": 233,
  4. "topId": "plumcherry",
  5. "subList": [
  6. {
  7. "S1": "A3"
  8. }
  9. ]
  10. }

After transformation :

  1. {
  2. "practice_loc": 334,
  3. "prac_num": 233,
  4. "topId": "plumcherry",
  5. "S1": "A3"
  6. }

4th case can be ;

Input :

  1. {
  2. "practice_loc": 987,
  3. "prac_num": 232,
  4. "topId": "rose",
  5. "subList": [
  6. {}
  7. ]
  8. }

After transformation :

  1. {
  2. "practice_loc": 987,
  3. "prac_num": 232,
  4. "topId": "rose"
  5. }

How can I write only one jolt which can cover all these cases and return the transformed data with whichever type of input it get

答案1

得分: 2

以下是翻译好的内容:

您可以使用以下转换

  1. [
  2. { // 通过上一级对象包装节点对元素进行分组
  3. "operation": "shift",
  4. "spec": {
  5. "subList": {
  6. "*": {
  7. "@2|@": "&3_&1" // 通过@2选择来自相同级别的“sublist”的外部元素,同时@返回该数组的值
  8. }
  9. }
  10. }
  11. },
  12. { // 摆脱包装
  13. "operation": "shift",
  14. "spec": {
  15. "*": {
  16. "*": {
  17. "*": "[#3].&"
  18. }
  19. }
  20. }
  21. },
  22. { // 摆脱额外生成的数组,即“subList”
  23. "operation": "remove",
  24. "spec": {
  25. "*": {
  26. "subList": ""
  27. }
  28. }
  29. }
  30. ]
英文:

You can use the following transformation

  1. [
  2. { // group elements by upper level objects wrapper nodes
  3. "operation": "shift",
  4. "spec": {
  5. "subList": {
  6. "*": {
  7. "@2|@": "&3_&1" // pick the outer elements as well from the same level of "sublist" through use of @2 while @ returns the value from this array
  8. }
  9. }
  10. }
  11. },
  12. { // get rid of the wrappers
  13. "operation": "shift",
  14. "spec": {
  15. "*": {
  16. "*": {
  17. "*": "[#3].&"
  18. }
  19. }
  20. }
  21. },
  22. { // get rid of the extra generated array, namely "subList"
  23. "operation": "remove",
  24. "spec": {
  25. "*": {
  26. "subList": ""
  27. }
  28. }
  29. }
  30. ]

huangapple
  • 本文由 发表于 2023年6月8日 16:16:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76429897.html
匿名

发表评论

匿名网友

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

确定