英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论