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

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

How to convert nested data into linear data using jolt

问题

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

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

数据

第1种情况:

输入

{
  "practice_loc": 120,
  "prac_num": 234,
  "topId": "t1",
  "subList": [
    {
      "S1": "A1",
      "S2": "B1"
    },
    {
      "S1": "A2"
    }
  ]
}

转换后

{
    "practice_loc": 120,
    "prac_num": 234,
    "topId": "t1",
    "S1": "A1",
    "S2": "B1"
  },
  {
    "practice_loc": 120,
    "prac_num": 234,
    "topId": "t1",
    "S1": "A2"
 }

第2种情况可以是:

输入

{
  "practice_loc": 987,
  "prac_num": 232,
  "topId": "artica",
  "subList": [
    {
      "S1": "A5",
      "S2": "B7"
    }
  ]
}

转换后

{
  "practice_loc": 987,
  "prac_num": 232,
  "topId": "artica",
  "S1": "A5",
  "S2": "B7"
}

第3种情况可以是:

输入

{
  "practice_loc": 334,
  "prac_num": 233,
  "topId": "plumcherry",
  "subList": [
    {
      "S1": "A3"
    }
  ]
}

转换后

{
  "practice_loc": 334,
  "prac_num": 233,
  "topId": "plumcherry",
  "S1": "A3"
}

第4种情况可以是:

输入

{
  "practice_loc": 987,
  "prac_num": 232,
  "topId": "rose",
  "subList": [
    {}
  ]
}

转换后

{
  "practice_loc": 987,
  "prac_num": 232,
  "topId": "rose"
}

我该如何编写一个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 :

{
  "practice_loc": 120,
  "prac_num": 234,
  "topId": "t1",
  "subList": [
    {
      "S1": "A1",
      "S2": "B1"
    },
    {
      "S1": "A2"
    }
  ]
}

After transformation:

{
    "practice_loc": 120,
    "prac_num": 234,
    "topId": "t1",
    "S1": "A1",
    "S2": "B1"
  },
  {
    "practice_loc": 120,
    "prac_num": 234,
    "topId": "t1",
    "S1": "A2"
 }

2nd case can be;

Input:

{
  "practice_loc": 987,
  "prac_num": 232,
  "topId": "artica",
  "subList": [
    {
      "S1": "A5",
      "S2": "B7"
    }
  ]
}

After transformation :

{
  "practice_loc": 987,
  "prac_num": 232,
  "topId": "artica",
  "S1": "A5",
  "S2": "B7"
}

3rd case can be;

Input :

{
  "practice_loc": 334,
  "prac_num": 233,
  "topId": "plumcherry",
  "subList": [
    {
      "S1": "A3"
    }
  ]
}

After transformation :

{
  "practice_loc": 334,
  "prac_num": 233,
  "topId": "plumcherry",
  "S1": "A3"
}

4th case can be ;

Input :

{
  "practice_loc": 987,
  "prac_num": 232,
  "topId": "rose",
  "subList": [
    {}
  ]
}

After transformation :

{
  "practice_loc": 987,
  "prac_num": 232,
  "topId": "rose"
}

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

以下是翻译好的内容:

您可以使用以下转换

[
  { // 通过上一级对象包装节点对元素进行分组
    "operation": "shift",
    "spec": {
      "subList": {
        "*": {
          "@2|@": "&3_&1" // 通过@2选择来自相同级别的“sublist”的外部元素,同时@返回该数组的值
        }
      }
    }
  },
  { // 摆脱包装
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": "[#3].&"
        }
      }
    }
  },
  { // 摆脱额外生成的数组,即“subList”
    "operation": "remove",
    "spec": {
      "*": {
        "subList": ""
      }
    }
  }
]
英文:

You can use the following transformation

[
  { // group elements by upper level objects wrapper nodes
    "operation": "shift",
    "spec": {
      "subList": {
        "*": {
          "@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
        }
      }
    }
  },
  { // get rid of the wrappers
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": "[#3].&"
        }
      }
    }
  },
  { // get rid of the extra generated array, namely "subList"
    "operation": "remove",
    "spec": {
      "*": {
        "subList": ""
      }
    }
  }
]

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:

确定