JSON 转换:展平复杂的嵌套 JSON 包含带 JOLT 的空对象的数组
JSON Transformation: Flattening Complex Nested JSON Array including empty Objects with JOLT
我是 JOLT 转换的新手,我发现它在 JSON 转换中非常有用。但是当我遇到嵌套和复杂的 JSON 时,我感到困惑。
下面的JSON是一个嵌套的复杂Json数组,我需要把它转换成一个完全扁平的Json数组。
JSON 输入:
[
{
"Code": -1,
"Name": "All",
"PublisherTypes": [
{
"Code": 2,
"Name": "General",
"LetterTypes": []
},
{
"Code": 3,
"Name": "Financials",
"LetterTypes": []
}
]
},
{
"Code": 1,
"Name": "Information",
"PublisherTypes": [
{
"Code": 2,
"Name": "General",
"LetterTypes": [
{
"Id": 10,
"Name": "Long Term Info",
"Code": ""
},
{
"Id": 20,
"Name": "Short Term Info",
"Code": ""
}
]
},
{
"Code": 3,
"Name": "Financials",
"LetterTypes": [
{
"Id": 101,
"Name": "Total Income",
"Code": ""
},
{
"Id": 202,
"Name": "Total Taxes",
"Code": ""
}
]
}
]
},
{
"Code": 2,
"Name": "Reporting",
"PublisherTypes": [
{
"Code": 2,
"Name": "General",
"LetterTypes": [
{
"Id": 8,
"Name": "Monthly Reoprt",
"Code": ""
},
{
"Id": 58,
"Name": "Status Report",
"Code": ""
}
]
},
{
"Code": 3,
"Name": "Financials",
"LetterTypes": [
{
"Id": 170,
"Name": "Manager Level",
"Code": ""
},
{
"Id": 156,
"Name": "Expert Level",
"Code": ""
}
]
}
]
}
]
如您所见,我们有对象 "LetterTypes":[] 是空的,但在其他 Json 对象 "LetterTypes" 中 有自己的对象并且不为空。
下面的JSON是我的预期输出。
预期输出:
[
{
"Code": -1,
"Name": "All",
"PublisherCode": 2,
"PublisherName": "General",
"LetterId": "",
"LetterName": "",
"LetterCode": ""
},
{
"Code": -1,
"Name": "All",
"PublisherCode": 3,
"PublisherName": "Financials",
"LetterId": "",
"LetterName": "",
"LetterCode": ""
},
{
"Code": 1,
"Name": "Information",
"PublisherCode": 2,
"PublisherName": "General",
"LetterId": 10,
"LetterName": "Long Term Info",
"LetterCode": ""
},
{
"Code": 1,
"Name": "Information",
"PublisherCode": 2,
"PublisherName": "General",
"LetterId": 20,
"LetterName": "Short Term Info",
"LetterCode": ""
},
{
"Code": 1,
"Name": "Information",
"PublisherCode": 3,
"PublisherName": "Financials",
"LetterId": 101,
"LetterName": "Total Income",
"LetterCode": ""
},
{
"Code": 1,
"Name": "Information",
"PublisherCode": 3,
"PublisherName": "Financials",
"LetterId": 202,
"LetterName": "Total Taxes",
"LetterCode": ""
},
{
"Code": 2,
"Name": "Reporting",
"PublisherCode": 2,
"PublisherName": "General",
"LetterId": 8,
"LetterName": "Monthly Reoprt",
"LetterCode": ""
},
{
"Code": 2,
"Name": "Reporting",
"PublisherCode": 2,
"PublisherName": "General",
"LetterId": 58,
"LetterName": "Status Reoprt",
"LetterCode": ""
},
{
"Code": 2,
"Name": "Reporting",
"PublisherCode": 3,
"PublisherName": "Financials",
"LetterId": 170,
"LetterName": "Manager Level",
"LetterCode": ""
},
{
"Code": 2,
"Name": "Reporting",
"PublisherCode": 3,
"PublisherName": "Financials",
"LetterId": 156,
"LetterName": "Expert Level",
"LetterCode": ""
}
]
我需要的是 JOLT 规范 为我生成上面的输出,这样当 "LetterTypes" 为空时,它会显示在具有 Empty String("") 值的输出。那么,谁能为这个问题提供 JOLT 规范?
您可以在应用 modify 转换规范后使用以下 shift 转换,其中;
- toInteger 将带引号的值转换为不带引号的整数(如果可以),例如
"12"->12
或 "12.7"->12
,否则它会静默停止处理,例如不带引号的数组括号 [ ]
被认为是可转换的,而带有嵌入对象的数组当然不被认为是整数
- 我们在第二步中得到了单独的数组(或列表),我们想将它们组合在一个公共因素下。 &2表示遍历
{
两次达到两层,[&]表示将当前层的所有东西合并为数组。
,为了用默认的 null(""
) 值填充空数组 LetterTypes
数组的每个属性,例如
[
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": {
"*": {
"Letter*": ["=toInteger",
[
{
"Id": "",
"Name": "",
"Code": ""
}
]
]
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*Types": { // * wildcard represents one or multiple characters, here "*Types" stands for "PublisherTypes"
"*": {
"*Types": {
"*": {
"@(4,Code)": "Code",
"@(4,Name)": "Name", // going 4 levels up to grab the desired value
"@(2,Code)": "PublisherCode",
"@(2,Name)": "PublisherName", // going 2 levels up to grab the desired value
"*": "&(2,1)&" // all attributes under the "LetterTypes" arrays, in &(2,1)& : 2 is for going 2 levels up, 1 represents grabbing the piece("Letter") where * is substituted, and the last & represents the value of the current key name("Code" or "Name")
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": {
"@": "[&].&2"
}
}
}
}
]
所有属性都累积在最里面的部分,在整个JSON值中出现次数最多的部分。
在第一个规范中,确定了所有包含 10 个组件的单独数组,并将它们移动到第二个规范中的适当对象。
我是 JOLT 转换的新手,我发现它在 JSON 转换中非常有用。但是当我遇到嵌套和复杂的 JSON 时,我感到困惑。
下面的JSON是一个嵌套的复杂Json数组,我需要把它转换成一个完全扁平的Json数组。
JSON 输入:
[
{
"Code": -1,
"Name": "All",
"PublisherTypes": [
{
"Code": 2,
"Name": "General",
"LetterTypes": []
},
{
"Code": 3,
"Name": "Financials",
"LetterTypes": []
}
]
},
{
"Code": 1,
"Name": "Information",
"PublisherTypes": [
{
"Code": 2,
"Name": "General",
"LetterTypes": [
{
"Id": 10,
"Name": "Long Term Info",
"Code": ""
},
{
"Id": 20,
"Name": "Short Term Info",
"Code": ""
}
]
},
{
"Code": 3,
"Name": "Financials",
"LetterTypes": [
{
"Id": 101,
"Name": "Total Income",
"Code": ""
},
{
"Id": 202,
"Name": "Total Taxes",
"Code": ""
}
]
}
]
},
{
"Code": 2,
"Name": "Reporting",
"PublisherTypes": [
{
"Code": 2,
"Name": "General",
"LetterTypes": [
{
"Id": 8,
"Name": "Monthly Reoprt",
"Code": ""
},
{
"Id": 58,
"Name": "Status Report",
"Code": ""
}
]
},
{
"Code": 3,
"Name": "Financials",
"LetterTypes": [
{
"Id": 170,
"Name": "Manager Level",
"Code": ""
},
{
"Id": 156,
"Name": "Expert Level",
"Code": ""
}
]
}
]
}
]
如您所见,我们有对象 "LetterTypes":[] 是空的,但在其他 Json 对象 "LetterTypes" 中 有自己的对象并且不为空。
下面的JSON是我的预期输出。
预期输出:
[
{
"Code": -1,
"Name": "All",
"PublisherCode": 2,
"PublisherName": "General",
"LetterId": "",
"LetterName": "",
"LetterCode": ""
},
{
"Code": -1,
"Name": "All",
"PublisherCode": 3,
"PublisherName": "Financials",
"LetterId": "",
"LetterName": "",
"LetterCode": ""
},
{
"Code": 1,
"Name": "Information",
"PublisherCode": 2,
"PublisherName": "General",
"LetterId": 10,
"LetterName": "Long Term Info",
"LetterCode": ""
},
{
"Code": 1,
"Name": "Information",
"PublisherCode": 2,
"PublisherName": "General",
"LetterId": 20,
"LetterName": "Short Term Info",
"LetterCode": ""
},
{
"Code": 1,
"Name": "Information",
"PublisherCode": 3,
"PublisherName": "Financials",
"LetterId": 101,
"LetterName": "Total Income",
"LetterCode": ""
},
{
"Code": 1,
"Name": "Information",
"PublisherCode": 3,
"PublisherName": "Financials",
"LetterId": 202,
"LetterName": "Total Taxes",
"LetterCode": ""
},
{
"Code": 2,
"Name": "Reporting",
"PublisherCode": 2,
"PublisherName": "General",
"LetterId": 8,
"LetterName": "Monthly Reoprt",
"LetterCode": ""
},
{
"Code": 2,
"Name": "Reporting",
"PublisherCode": 2,
"PublisherName": "General",
"LetterId": 58,
"LetterName": "Status Reoprt",
"LetterCode": ""
},
{
"Code": 2,
"Name": "Reporting",
"PublisherCode": 3,
"PublisherName": "Financials",
"LetterId": 170,
"LetterName": "Manager Level",
"LetterCode": ""
},
{
"Code": 2,
"Name": "Reporting",
"PublisherCode": 3,
"PublisherName": "Financials",
"LetterId": 156,
"LetterName": "Expert Level",
"LetterCode": ""
}
]
我需要的是 JOLT 规范 为我生成上面的输出,这样当 "LetterTypes" 为空时,它会显示在具有 Empty String("") 值的输出。那么,谁能为这个问题提供 JOLT 规范?
您可以在应用 modify 转换规范后使用以下 shift 转换,其中;
- toInteger 将带引号的值转换为不带引号的整数(如果可以),例如
"12"->12
或"12.7"->12
,否则它会静默停止处理,例如不带引号的数组括号[ ]
被认为是可转换的,而带有嵌入对象的数组当然不被认为是整数 - 我们在第二步中得到了单独的数组(或列表),我们想将它们组合在一个公共因素下。 &2表示遍历
{
两次达到两层,[&]表示将当前层的所有东西合并为数组。
,为了用默认的 null(""
) 值填充空数组 LetterTypes
数组的每个属性,例如
[
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": {
"*": {
"Letter*": ["=toInteger",
[
{
"Id": "",
"Name": "",
"Code": ""
}
]
]
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*Types": { // * wildcard represents one or multiple characters, here "*Types" stands for "PublisherTypes"
"*": {
"*Types": {
"*": {
"@(4,Code)": "Code",
"@(4,Name)": "Name", // going 4 levels up to grab the desired value
"@(2,Code)": "PublisherCode",
"@(2,Name)": "PublisherName", // going 2 levels up to grab the desired value
"*": "&(2,1)&" // all attributes under the "LetterTypes" arrays, in &(2,1)& : 2 is for going 2 levels up, 1 represents grabbing the piece("Letter") where * is substituted, and the last & represents the value of the current key name("Code" or "Name")
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": {
"@": "[&].&2"
}
}
}
}
]
所有属性都累积在最里面的部分,在整个JSON值中出现次数最多的部分。
在第一个规范中,确定了所有包含 10 个组件的单独数组,并将它们移动到第二个规范中的适当对象。