问题描述
我有以下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"
}
我想要什么:
- 删除
fieldMask
参数,所以我不需要将结果作为数组. - 删除
结果
和customerCient
.所以我的JSON应该看起来像这样:
- remove
fieldMask
parameter, so i don't need results as array. - remove
results
andcustomerCient
.So my JSON should look like this:
{
"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"
}
要删除 results
,我正在使用拆分值为 $.results
的 SplitJson
.现在,我只能使用JOLT规范对其进行修改.我该如何仅使用 JoltTransformJSON
来描述上述操作?
To remove results
i'm using SplitJson
with split value: $.results
. And now i'm stuck at modifying it with JOLT specification.How can I do described above actions with only JoltTransformJSON
?
推荐答案
好,所以我相信JOLT只能输出一个对象,因此在一个JOLT中不可能将数组的每个元素转换为唯一对象.但是,您可以在不将FlowFile数据放入属性"中的情况下获得大部分访问权限.
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. However, you can get most of the way there without putting FlowFile data in to Attributes.
我们可以:
- 删除
fieldMask
- 删除
结果
数组级别
使用它来测试JOLT: https://jolt-demo.appspot.com/#起始
Use this to test out JOLTs: https://jolt-demo.appspot.com/#inception
假设您的JSON实际上是有效的,那么:
Assuming your JSON is actually valid, so:
{
"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"
}
您可以使用以下JOLT规范:
You could use this JOLT spec:
[
{
"operation": "remove",
"spec": {
"fieldMask": ""
}
},
{
"operation": "shift",
"spec": {
"results": {
"*": {
"customerClient": "customerClient-&1"
}
}
}
}
]
哪个会给您以下结果:
{
"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"
}
}
因此,您可以仅使用JOLT将JSON转换为平面结构,然后可以使用SplitJSON分解每个对象(如果需要).
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).
您应该考虑使用Records而不是SplitJSON,这可能会更有效率.
You should consider using Records instead of SplitJSON, this would probably be more efficient.
阅读记录:
- https://blogs.apache.org/nifi/entry/record-directional-data-with-nifi
- https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-standard-nar/1.12.1/org.apache.nifi.processors.standard.UpdateRecord/index.html
- https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-record-serialization-services-nar/1.12.1/org.apache.nifi.json.JsonTreeReader/index.html
另一个有趣的替代方法可能是 ScriptedTransformRecord
Another interesting alternative could be ScriptedTransformRecord
这篇关于NiFi:JoltTransformJSON规范的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!