JOLT变换-嵌套Json到具有多行的平面Json

3pvhb19x  于 2023-05-02  发布在  其他
关注(0)|答案(2)|浏览(239)

我正在尝试编写一个Jolt转换规范,将嵌套的JSON转换为带行的平面JSON。我们的目标是创建一个可以同时处理输入数据1和输入数据2的规范。
输入数据1

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

输入数据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
          }
        ]
      }
    ]
  }
]

我要输入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
  }
]

而对于输入2,输出应采用以下格式

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

mwkjh3gx1#

您可以使用此规范:

[
  {
    "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可以帮助你实现你想要的输出。否则,您可以使用其他工具或库找到另一种方法。

tzcvj98z

tzcvj98z2#

您可以使用以下规范

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

相关问题