英文:
How to split json array into sub array using jolt specifcation in Apache Nifi?
问题
我正在尝试为预期输出准备一个jolt
规范,请帮助我。
注意:
预期输出:如果信用额度在数组中具有多个字段,则JSON将拆分为子JSON,其中InvoiceMonth
将与输入相同,但在第一个子JSON中,InvoiceMonth
的值保持不变,其余子JSON的InvoiceMonth
值需要为0。
输入:
[
{
"InvoiceMonth": "202306",
"credit": {
"name": [
"7828credit:0",
"2738credit:0"
],
"amount": [
"2.5E-5",
"5.0E-5"
],
"full_name": [
"Committed USAGE",
"Committed USAGE "
],
"id": [
"7822credit:0",
"2732credit:0"
],
"type": [
"COMMITTEDDISCOUNT",
"COMMITTEDDISCOUNTBASE"
]
},
"resource_name": "projects-2",
"resource_global_name": "//https.googleapis.com/",
"label": ""
}
]
输出:
[
{
"InvoiceMonth": "202306",
"credit": {
"name": "7822credit:0",
"amount": "2.5E-5",
"full_name": "Committed",
"id": "7822credit:0",
"type": "COMMITTEDUSAGE"
},
"resource_name": "projects-2",
"resource_global_name": "//https.googleapis.com/",
"label": ""
},
{
"InvoiceMonth": "0",
"credit": {
"name": "2738credit:0",
"amount": "5.0E-5",
"full_name": "Committed ",
"id": "2738_credit:0",
"type": "COMMITTEDUSAGEDISCOUNT"
},
"resource_name": "projects-2",
"resource_global_name": "//https.googleapis.com/",
"label": ""
}
]
如果您需要进一步的帮助,请告诉我。
英文:
I am tring to prepare a jolt
specification for the expected output, please help me out.
Note:
The expected output: where credit is in array with multiple field, then json will split into subjson where InvoiceMonth
will be same as input only but in 1st subjson the InvoiceMonth
value has as it is remaining subjsons InvoiceMonth
values needs to be 0.
Input:
[
{
"InvoiceMonth": "202306",
"credit": {
"name": [
"7828credit:0",
"2738credit:0"
],
"amount": [
"2.5E-5",
"5.0E-5"
],
"full_name": [
"Committed USAGE",
"Committed USAGE "
],
"id": [
"7822credit:0",
"2732credit:0"
],
"type": [
"COMMITTEDDISCOUNT",
"COMMITTEDDISCOUNTBASE"
]
},
"resource_name": "projects-2",
"resource_global_name": "//https.googleapis.com/",
"label": ""
}
]
Output:
[
{
"InvoiceMonth": "202306",
"credit": {
"name": "7822credit:0",
"amount": "2.5E-5",
"full_name": "Committed",
"id": "7822credit:0",
"type": "COMMITTEDUSAGE"
},
"resource_name": "projects-2",
"resource_global_name": "//https.googleapis.com/",
"label": ""
},
{
"InvoiceMonth": "0",
"credit": {
"name": "2738credit:0",
"amount": "5.0E-5",
"full_name": "Committed ",
"id": "2738_credit:0",
"type": "COMMITTEDUSAGEDISCOUNT"
},
"resource_name": "projects-2",
"resource_global_name": "//https.googleapis.com/",
"label": ""
}
]
答案1
得分: 1
以下是翻译好的部分:
[
{
"operation": "shift",
"spec": {
"*": {
"credit": {
"*": {
"0": {
"@3,InvoiceMonth": "[#2].InvoiceMonth",
"@": "[#2].&3.&2",
"@3,resource_name": "[#2].resource_name",
"@3,resource_global_name": "[#2].resource_global_name",
"@3,label": "[#2].label"
},
"*": {
"#0": "[#2].InvoiceMonth",
"@": "[#2].&3.&2",
"@3,resource_name": "[#2].resource_name",
"@3,resource_global_name": "[#2].resource_global_name",
"@3,label": "[#2].label"
}
}
}
}
}
},
{
"operation": "cardinality",
"spec": {
"*": {
"*": "ONE"
}
}
}
]
希望这有帮助!如果你需要更多翻译,请随时告诉我。
英文:
You can use the following spec
[
{
"operation": "shift",
"spec": {
"*": {
"credit": {
"*": {
"0": {
"@3,InvoiceMonth": "[#2].InvoiceMonth", // go up the tree three levels to reach the level of the attribute and grab its value
"@": "[#2].&3.&2",
"@3,resource_name": "[#2].resource_name",
"@3,resource_global_name": "[#2].resource_global_name",
"@3,label": "[#2].label"
},
"*": {
"#0": "[#2].InvoiceMonth", // all values expect for the first one for the attribute is set to zero
"@": "[#2].&3.&2",
"@3,resource_name": "[#2].resource_name",
"@3,resource_global_name": "[#2].resource_global_name",
"@3,label": "[#2].label"
}
}
}
}
}
},
{ // pick only one component from the reformed arrays with the multiple repeating components
"operation": "cardinality",
"spec": {
"*": {
"*": "ONE"
}
}
}
]
where the main idea is looping through the arrays under "credit"
object, and then adding the other attributes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论