英文:
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对象转换为所需的扁平格式,您可以执行以下步骤:
-
首先,您需要定义一个JOLT规范,该规范描述了如何将输入JSON转换为所需的输出格式。在这个规范中,您可以使用JOLT操作来重命名字段、提取值等。
-
然后,您可以使用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
没有直接的替代函数,但可以通过使用连续的 split 和 join 函数在 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
}
}
}
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论