Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

Nifi : Array of json elements to list of json elements

avatar
Explorer

I have a json input which structure is as follows:

[
  {
    "line": [
      {
        "element1": 4.908278133674332,
        "element2": 45.8293948360587
      },
      {
        "element1": 4.90838573779603,
        "element2": 45.82938855483125
      }
    ],
    "coord": 1749,
    "xenius": "LYO01749",
    "label": "R M. BASTIE",
    "date": "2018-12-17"
  },
  {
    "line": [
      {
        "element1": 7.908278133674332,
        "element2": 8.8293948360587
      },
      {
        "element1": 4.555,
        "element2": 42.34534
      }
    ],
    "coord": 223,
    "xenius": "LYO01749",
    "label": "R M. BASTIE",
    "date": "2018-12-17"
  }
]

And I would like to convert it into a list of json elements. Something like:

{
    "line": [
      {
        "element1": 4.908278133674332,
        "element2": 45.8293948360587
      },
      {
        "element1": 4.90838573779603,
        "element2": 45.82938855483125
      }
    ],
    "coord": 1749,
    "xenius": "LYO01749",
    "label": "R M. BASTIE",
    "date": "2018-12-17"
  }
{
    "line": [
      {
        "element1": 7.908278133674332,
        "element2": 8.8293948360587
      },
      {
        "element1": 4.555,
        "element2": 42.34534
      }
    ],
    "coord": 223,
    "xenius": "LYO01749",
    "label": "R M. BASTIE",
    "date": "2018-12-17"
  }

How could I do that with a json or jolt transformation, and not using SplitRecord?

Please note that I just want to get all the elements of the array and output them as a list of json elements, without the parent array.

Thanks in advance.

1 ACCEPTED SOLUTION

avatar
Master Guru

You can use ConvertRecord for this, your JsonRecordSetWriter would have its Output Grouping property set to "One Line Per Object". The individual objects won't be pretty printed in the output as you have above, but it will remove the array braces as well as the comma delimiter, and just output each element of the above array as a JSON object on its own line.

View solution in original post

2 REPLIES 2

avatar
Master Guru

You can use ConvertRecord for this, your JsonRecordSetWriter would have its Output Grouping property set to "One Line Per Object". The individual objects won't be pretty printed in the output as you have above, but it will remove the array braces as well as the comma delimiter, and just output each element of the above array as a JSON object on its own line.

avatar
Explorer

Output Grouping... magical property 🙂

That did the trick. Thanks a lot Matt!