将字段从 json 对象移动到有条件的另一个对象

问题描述 投票:0回答:1

我有以下 JSON 结构:

{
  "1": {
    "precedingId": "0",
    "value": "A"
  },
  "2": {
    "precedingId": "1",
    "value": "B"
  },
  "3": {
    "precedingId": "2",
    "value": "C"
  }
}

我正在尝试使用 Jolt 获得以下输出:

{
  "1": {
    "precedingId": "0",
    "precedingValue" : null, // null because there is no 0 objet
    "value": "A"
  },
  "2": {
    "precedingId": "1",
    "precedingValue" : "A",
    "value": "B"
  },
  "3": {
    "precedingId": "2",
    "precedingValue" : "B"
    "value": "C"
  }
}

对于每个对象,以predictionId为key,查找上面1层是否有对应的对象,如果有则取value作为previousValue放入当前对象中。

我尝试了几天的几个规格,但我认为在 Jolt 中执行这种逻辑可能是不可能的?

json apache-nifi jolt
1个回答
0
投票

您可以使用以下转换规范:

[
  {//group the attributes by inner "precedingId" values of each object
   //by @1,precedingId prefixes
   //except the precedingValues from the other objects    
   //in order to transfer them from their original object
   //by using &1 prefix 
    "operation": "shift",
    "spec": {
      "*": {
        "$": "@1,precedingId.id",
        "value": ["@1,precedingId.value", "&1.precedingValue"],
        "precedingId": "@1,precedingId.&"
      }
    }
  },
  {//this time group the attributes by the generated "id" values
    "operation": "shift",
    "spec": {
      "*": {
        "id": { "": "" }, //get rid of temporarily generated "id" values
        "*": "@1,id.&"
      }
    }
  },
  {//set the missing value
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "~precedingValue": null// ~ operator means if this attribute does not exist or is null, then make it be null
      }
    }
  }
]

网站上的演示使用v0.1.1的Jolt Transform演示是:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.