JOLT 转换 – 将嵌套的 JSON 转换为具有多行的扁平 JSON

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

JOLT Transform - Nested Json to Flat Json with multiple rows

问题

[
  {
    "Id": "123",
    "Name": "tets",
    "records.aaa": "123",
    "records.test.zzz": 987,
    "records.test.yyy": 123
  },
  {
    "Id": "123",
    "Name": "tets",
    "records.aaa": "123",
    "records.test.zzz": 345,
    "records.test.yyy": 678
  }
]
[
  {
    "Id": "123",
    "Name": "tets",
    "records.aaa": "123",
    "records.test.zzz": 987,
    "records.test.yyy": 123
  },
  {
    "Id": "123",
    "Name": "tets",
    "records.aaa": "123",
    "records.test.zzz": 345,
    "records.test.yyy": 678
  },
  {
    "Id": "1234",
    "Name": "tets2",
    "records.aaa": "123",
    "records.test.zzz": 987,
    "records.test.yyy": 123
  },
  {
    "Id": "1234",
    "Name": "tets2",
    "records.aaa": "123",
    "records.test.zzz": 345,
    "records.test.yyy": 678
  },
  {
    "Id": "1234",
    "Name": "tets2",
    "records.aaa": "12345",
    "records.test.zzz": 456,
    "records.test.yyy": 567
  }
]
英文:

I am attempting to write a Jolt transformation specification to convert nested JSON to flat JSON with rows. The goal is to create a single spec that can handle both Input Data 1 and Input Data 2.

Input Data 1

{
  "Id": "123",
  "Name": "tets",
  "records": [
    {
      "aaa": "123",
      "test": [
        {
          "zzz": 987,
          "yyy": 123
        },
        {
          "zzz": 345,
          "yyy": 678
        }
      ]
    }
  ]
}

Input Data 2

[
  {
    "Id": "123",
    "Name": "tets",
    "records": [
      {
        "aaa": "123",
        "test": [
          {
            "zzz": 987,
            "yyy": 123
          },
          {
            "zzz": 345,
            "yyy": 678
          }
        ]
      }
    ]
  },
  {
    "Id": "1234",
    "Name": "tets2",
    "records": [
      {
        "aaa": "123",
        "test": [
          {
            "zzz": 987,
            "yyy": 123
          },
          {
            "zzz": 345,
            "yyy": 678
          }
        ]
      },
      {
        "aaa": "12345",
        "test": [
          {
            "zzz": 456,
            "yyy": 567
          }
        ]
      }
    ]
  }
]

I want the output for input 1

[
  {
    "Id": "123",
    "Name": "tets",
    "records.aaa": "123",
    "records.test.zzz": 987,
    "records.test.yyy": 123
  },
  {
    "Id": "123",
    "Name": "tets",
    "records.aaa": "123",
    "records.test.zzz": 345,
    "records.test.yyy": 678
  }
]

And for input 2, the output should be in the below format

[
  {
    "Id": "123",
    "Name": "tets",
    "records.aaa": "123",
    "records.test.zzz": 987,
    "records.test.yyy": 123
  },
  {
    "Id": "123",
    "Name": "tets",
    "records.aaa": "123",
    "records.test.zzz": 345,
    "records.test.yyy": 678
  },
  {
    "Id": "1234",
    "Name": "tets2",
    "records.aaa": "123",
    "records.test.zzz": 987,
    "records.test.yyy": 123
  },
  {
    "Id": "1234",
    "Name": "tets2",
    "records.aaa": "123",
    "records.test.zzz": 345,
    "records.test.yyy": 678
  },
  {
    "Id": "1234",
    "Name": "tets2",
    "records.aaa": "12345",
    "records.test.zzz": 456,
    "records.test.yyy": 567
  }
]

答案1

得分: 1

你可以使用以下规范

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@": "type1",
        "Id": "input1"
      },
      "@": "type2[]",
      "Id": "input2"
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "input1?": "@1,type1",
      "input2?": "@1,type2"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "input*": {
        "*": {
          "records": {
            "*": {
              "test": {
                "*": {
                  "@4,Id": "&5\\.&3\\.&1.Id",
                  "@4,Name": "&5\\.&3\\.&1.Name",
                  "@2,aaa": "&5\\.&3\\.&1.&4\\.aaa",
                  "*": "&5\\.&3\\.&1.&4\\.&2\\.&"
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[#2].&"
      }
    }
  }
]
英文:

You can use the following spec

[
  { // prepare to combine the two types of inputs 
    "operation": "shift",
    "spec": {
      "*": {
        "@": "type1",
        "Id": "input1"
      },
      "@": "type2[]", // convert to array this one as well by nesting within square breackets
      "Id": "input2"
    }
  },
  { // distinguish and pick the presented input to be processed within the next specs
    "operation": "modify-overwrite-beta",
    "spec": {
      "input1?": "@1,type1", // check the existence of the respective attributes by using ? at the end
      "input2?": "@1,type2"
    }
  },
  { // determine the independent objects of the reformed array
    "operation": "shift",
    "spec": {
      "input*": {
        "*": {
          "records": {
            "*": {
              "test": {
                "*": {
                  "@4,Id": "&5\\.&3\\.&1.Id",
                  "@4,Name": "&5\\.&3\\.&1.Name",
                  "@2,aaa": "&5\\.&3\\.&1.&4\\.aaa",
                  "*": "&5\\.&3\\.&1.&4\\.&2\\.&"
                }
              }
            }
          }
        }
      }
    }
  },
  { // get rid of object keys
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[#2].&"
      }
    }
  }
]

答案2

得分: 1

以下是翻译好的部分:

[
  {
    "operation": "shift",
    "spec": {
      "@": "temp"
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "temp": "MANY"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "records": {
            "*": {
              "test": {
                "*": {
                  "@(5,[&4].Id)": "[&5][&3][&1].Id",
                  "@(5,[&4].Name)": "[&5][&3][&1].Name",
                  "@(2,aaa)": "[&5][&3][&1].&4\\.aaa",
                  "*": "[&5][&3][&1].&4\\.&2\\.&"
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": ""
        }
      }
    }
  }
]

注意: 如果您有更多嵌套的值,您应该确保知道它们的数量,然后可以使用JOLT来实现所需的输出。否则,您可以使用其他工具或库找到另一种方法。

英文:

You can use this spec:

[
  {
    "operation": "shift",
    "spec": {
      "@": "temp"
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "temp": "MANY"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "records": {
            "*": {
              "test": {
                "*": {
                  "@(5,[&4].Id)": "[&5][&3][&1].Id",
                  "@(5,[&4].Name)": "[&5][&3][&1].Name",
                  "@(2,aaa)": "[&5][&3][&1].&4\\.aaa",
                  "*": "[&5][&3][&1].&4\\.&2\\.&"
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": ""
        }
      }
    }
  }
]

NOTE: If you have more nested values, you should sure about how many they are, then JOLT can help you achieve your desired output. Otherwise, you can find another way with other tools or libraries.

huangapple
  • 本文由 发表于 2023年4月13日 22:13:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006472.html
匿名

发表评论

匿名网友

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

确定