简体   繁体   English

NiFi:JoltTransformJSON 规范

[英]NiFi: JoltTransformJSON specification

I have following JSON:我有以下 JSON:

{
  "results": [
    {
      "customerClient": {
        "resourceName": "customers/7876562723/customerClients/8506630423",
        "clientCustomer": "customers/8506630423",
        "hidden": false,
        "level": "1",
        "manager": false,
        "descriptiveName": "BMW",
        "id": "85061423"
      }
    },
    {
      "customerClient": {
        "resourceName": "customers/7876562723/customerClients/6736523142",
        "clientCustomer": "customers/6736523142",
        "hidden": false,
        "level": "1",
        "manager": true,
        "descriptiveName": "Mercedes",
        "id": "67363142"
      }
    }
}
  ],
  "fieldMask": "customerClient.clientCustomer,customerClient.hidden,customerClient.level,customerClient.descriptiveName,customerClient.id,customerClient.manager"
}

What i want:我想要的是:

  1. remove fieldMask parameter, so i don't need results as array.删除fieldMask参数,所以我不需要将结果作为数组。
  2. remove results and customerCient .删除resultscustomerCient So my JSON should look like this:所以我的 JSON 应该是这样的:
{
   "resourceName": "customers/7876562723/customerClients/8506630423",
   "clientCustomer": "customers/8506630423",
   "hidden": false,
   "level": "1",
   "manager": false,
   "descriptiveName": "BMW",
   "id": "85061423"
},
{
   "resourceName": "customers/7876562723/customerClients/6736523142",
   "clientCustomer": "customers/6736523142",
   "hidden": false,
   "level": "1",
   "manager": true,
   "descriptiveName": "Mercedes",
   "id": "67363142"
}

To remove results i'm using SplitJson with split value: $.results .要删除results ,我使用具有拆分值的SplitJson$.results And now i'm stuck at modifying it with JOLT specification.现在我坚持使用 JOLT 规范对其进行修改。 How can I do described above actions with only JoltTransformJSON ?如何仅使用JoltTransformJSON进行上述操作?

To start with - you json is wrong, you have an extra }.首先 - 你 json 是错误的,你有一个额外的}。

Not Using jolt不使用颠簸

1 - generate data 1 - 生成数据

2 - evaluatejson with $.results.[*].customerClient as attribute results 2 - 使用$.results.[*].customerClient作为属性results评估json

3 - replacetext with ${results:replace(']',''):replace('[','')} 3 - 用${results:replace(']',''):replace('[','')}

this will giive you这会给你

{
    "resourceName": "customers/7876562723/customerClients/8506630423",
    "clientCustomer": "customers/8506630423",
    "hidden": false,
    "level": "1",
    "manager": false,
    "descriptiveName": "BMW",
    "id": "85061423"
},
{
    "resourceName": "customers/7876562723/customerClients/6736523142",
    "clientCustomer": "customers/6736523142",
    "hidden": false,
    "level": "1",
    "manager": true,
    "descriptiveName": "Mercedes",
    "id": "67363142"
}

Ok, so I believe JOLT can only output one object, so transforming each element of the array to unique objects wouldn't be possible in one JOLT.好的,所以我相信 JOLT 只能 output 一个 object,因此在一个 JOLT 中不可能将数组的每个元素转换为唯一的对象。 However, you can get most of the way there without putting FlowFile data in to Attributes.但是,您可以在不将 FlowFile 数据放入 Attributes 的情况下获得大部分方法。

We can:我们可以:

  • delete fieldMask删除fieldMask
  • Remove the results array level删除results数组级别

Use this to test out JOLTs: https://jolt-demo.appspot.com/#inception使用它来测试 JOLT: https://jolt-demo.appspot.com/#inception

Assuming your JSON is actually valid, so:假设您的 JSON 实际上是有效的,所以:

{
  "results": [
    {
      "customerClient": {
        "resourceName": "customers/7876562723/customerClients/8506630423",
        "clientCustomer": "customers/8506630423",
        "hidden": false,
        "level": "1",
        "manager": false,
        "descriptiveName": "BMW",
        "id": "85061423"
      }
    },
    {
      "customerClient": {
        "resourceName": "customers/7876562723/customerClients/6736523142",
        "clientCustomer": "customers/6736523142",
        "hidden": false,
        "level": "1",
        "manager": true,
        "descriptiveName": "Mercedes",
        "id": "67363142"
      }
    }
  ],
  "fieldMask": "customerClient.clientCustomer,customerClient.hidden,customerClient.level,customerClient.descriptiveName,customerClient.id,customerClient.manager"
}

You could use this JOLT spec:你可以使用这个 JOLT 规范:

[
  {
    "operation": "remove",
    "spec": {
      "fieldMask": ""
    }
  },
  {
    "operation": "shift",
    "spec": {
      "results": {
        "*": {
          "customerClient": "customerClient-&1"
        }
      }
    }
  }
]

Which gives you following result:这给了你以下结果:

{
  "customerClient-0" : {
    "resourceName" : "customers/7876562723/customerClients/8506630423",
    "clientCustomer" : "customers/8506630423",
    "hidden" : false,
    "level" : "1",
    "manager" : false,
    "descriptiveName" : "BMW",
    "id" : "85061423"
  },
  "customerClient-1" : {
    "resourceName" : "customers/7876562723/customerClients/6736523142",
    "clientCustomer" : "customers/6736523142",
    "hidden" : false,
    "level" : "1",
    "manager" : true,
    "descriptiveName" : "Mercedes",
    "id" : "67363142"
  }
}

So you can transform your JSON to a flat structure using just the JOLT, then you could use SplitJSON to break each object up (if needed).因此,您可以仅使用 JOLT 将 JSON 转换为平面结构,然后您可以使用 SplitJSON 将每个 object 分解(如果需要)。

You should consider using Records instead of SplitJSON, this would probably be more efficient.您应该考虑使用 Records 而不是 SplitJSON,这可能会更有效。

Read about records:阅读有关记录:

Another interesting alternative could be ScriptedTransformRecord另一个有趣的选择可能是ScriptedTransformRecord

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM