Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
623 views
in Technique[技术] by (71.8m points)

Is it possible to create multiple JSON objects from single JSON using Jolt specification in NIFI

I have an input JSON:

{
  "flu": "flu1",
  "reportId": 11,
  "Name":"Transform"
}
 

Expected output:

{
  "flu": "flu1",
  "reportId": 11,
  "Name":"Transform",
  "fullname":"flu1-11-Transform"
}
{
 "type":"relationship",
  "id":"flu1-11-Transform"
}

Spec:

[{
  "operation":"modify-default-beta",
  "spec":{
    "fullname": "=concat(@(1,flu),'-',@(1,reportId),'-',@(1,Name))"
  }
}]

The above spec works for the first JSON object. Is it possible to create two JSON objects like shown in the expected output?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can only output one top-level object with JOLT, but that object can be an array (your desired output is not valid JSON as-is). If you want a two-element array containing those objects, you can use the following spec:

[{
    "operation": "modify-default-beta",
    "spec": {
      "fullname": "=concat(@(1,flu),'-',@(1,reportId),'-',@(1,Name))"
    }
 },
  {
    "operation": "shift",
    "spec": {
      "*": "[0].&",
      "@(1,fullname)": "[1].id",
      "#relationship": "[1].type"
    }
}]

If you need two JSON objects separated by whitespace, since you've tagged Apache NiFi in this question, you can use JoltTransformRecord with a JsonRecordSetWriter that has the Output Grouping property set to One Line Per Object. The formatting won't be exactly the same as above (each object will be on a single line) but it will be two JSON objects separated by whitespace (some systems accept/expect this format).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...