如何使用 JOLT 转换来展平具有数组值的 JSON 对象

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

How to flattend json object that has an array as a value with JOLT transformation

问题

以下是您提供的JSON对象的翻译:

{
  "type": "api.something, api.somethingElse",
  "Id": "d1537a72-2e46-29c0-b119-2bac07857ka9",
  "ItemId": "d1537a72-2e46-29c0-b119-2bac07869t8",
  "Amount": 50,
  "Increased": 10,
  "Type": "increasment",
  "isIncreasment": "true",
  "ItemId": "d1258672-3592-29c0-b119-2bac07857ka9",
  "testID": 105,
  "ListId": 18576,
  "Date": "2023-06-15T13:47:57.66"
}

关于如何使用JOLT转换将JSON对象转换为所需的扁平格式,您可以执行以下步骤:

  1. 首先,您需要定义一个JOLT规范,该规范描述了如何将输入JSON转换为所需的输出格式。在这个规范中,您可以使用JOLT操作来重命名字段、提取值等。

  2. 然后,您可以使用JOLT库或工具来应用这个规范。您可以将输入JSON和规范提供给JOLT,然后它将返回转换后的JSON。

要详细了解如何编写和应用JOLT规范,建议查看JOLT文档或教程,因为具体的规范将取决于您的输入数据结构和所需的输出结构。

英文:

I have an JSON object in following format:

{
  "$type": "api.something, api.somethingElse",
  "fromApi": {
    "$type": "api.something, api.somethingElse",
    "Id": "d1537a72-2e46-29c0-b119-2bac07857ka9",
    "ItemId": "d1537a72-2e46-29c0-b119-2bac07869t8",
    "Amount": 50,
    "Increased": 10,
    "Type": "increasment",
    "SourceData": "{\"isIncreasment\":true,\"ItemId\":\"d1258672-3592-29c0-b119-2bac07857ka9\",\"testID\":105,\"ListId\":18576}",
    "Date": "2023-06-12T13:47:57.66"
  }
}

and with JOLT transformation I want to get it in flatten format, like this:

{
  "type" : "api.something, api.somethingElse",
  "Id" : "d1537a72-2e46-29c0-b119-2bac07857ka9",
  "ItemId" : "a1537a72-2e46-29c0-b119-2bac07869t8",
  "Amount" : 50,
  "Increased" : 10,
  "Type" : "increasment",
  "isIncreasment" : "true",
  "ItemId" : "d1258672-3592-29c0-b119-2bac07857ka9",
  "testID" : 105,
  "ListId" : 18576,
  "Date": "2023-06-15T13:47:57.66"
}

Is this possible and how to do it?

答案1

得分: 1

没有直接的替代函数,但可以通过使用连续的 splitjoin 函数在 modify 转换中模拟,例如:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "fromApi": {
        "sd0": "=split('\"', @(1,SourceData))",
        "sd1": "=join('', @(1,sd0))",
        "sd2": "=split('{', @(1,sd1))",
        "sd3": "=join('', @(1,sd2))",
        "sd4": "=split('}', @(1,sd3))",
        "sd5": "=join('', @(1,sd4))",
        "SourceData": "=split(',', @(1,sd5))"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "fromApi": {
        "\$type": "others.type",
        "ItemId": "others.ItemId_old",
        "*": "others.&",
        "sd*":"sd.&",
        "SourceData": {
          "*": {
            "*:*": {
              "#k": "&2.&(1,1)",
              "#v": "&2.&(1,2)"
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "$": "&2.@0"
        }
      },
      "others": {
        "*": "&1.&"
      },
      "sd*": {
        "*": "&1.&"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "others": {
        "*": "&"
      },
      "*": {
        "@v": "@k"
      }
    }
  }
]

如果您需要进一步的解释或帮助,请随时提出。

英文:

There's no direct replacement function, but might be simulated through use of successive split and join functions within modify transformation(s) such as

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "fromApi": {
        "sd0": "=split('\"',@(1,SourceData))",
        "sd1": "=join('',@(1,sd0))",
        "sd2": "=split('\\{',@(1,sd1))",
        "sd3": "=join('',@(1,sd2))",
        "sd4": "=split('\\}',@(1,sd3))",
        "sd5": "=join('',@(1,sd4))",
        "SourceData": "=split(',',@(1,sd5))"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "fromApi": {
        "\\$type": "others.type",
        "ItemId": "others.ItemId_old", // need to rename, otherwise will yield an array with 
                                       // two components as an object might not have two attributes 
                                       // with the common names
        "*": "others.&",
        "sd*":"sd.&",
        "SourceData": {
          "*": {
            "*:*": { // separate the keys and values
              "#k": "&2.&(1,1)", // piece extracted from "*:*" representation after reaching 1 upper level, till ":", eg. 1st asterisk
              "#v": "&2.&(1,2)"  // piece extracted from "*:*" representation after reaching 1 upper level, till ":", eg. 2nd asterisk
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "$": "&2.@0" // exchange keys vs. values
        }
      },
      "others": {
        "*": "&1.&"
      },
      "sd*": {
        "*": "&1.&"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "others": {
        "*": "&"
      },
      "*": {
        "@v": "@k" // match derived keys and values as pairs
      }
    }
  }
]

huangapple
  • 本文由 发表于 2023年6月15日 21:26:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482981.html
匿名

发表评论

匿名网友

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

确定