Search code examples
jsonapache-nifijolt

Jolt expression to merge 2D array


So I have geo Coordinates and I want to create polygon string that i can insert into Postgres as geometry type. I am using apache Nifi.

Input:

[
  [
    13,
    52
  ],
  [
    13,
    55
  ],
  [
    14,
    15
  ]
]

Expected Output:

POLYGON((13 52, 13 55, 14 15))

I tried with below JOLT but no luck:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[&1].[&0]"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": "=join(' ',@(1,&))"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=concat('POLYGON((',@(1,2),'))')"
    }
  }
]

Solution

  • You can convert the current one to this :

    [
      { // form independent arrays with keys of indexes taken from the outermost array
        "operation": "shift",
        "spec": {
          "*": {
            "*": "&1"
          }
        }
      },
      { // stringify those arrays
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": "=join(' ',@(1,&))"
        }
      },
      { // generate an array namely "Coord"
        "operation": "shift",
        "spec": {
          "*": {
            "@": "Coord"
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "Coord": "=join(' ,',@(1,&))",
          "*": "=concat('POLYGON((',@(1,&),'))')"
        }
      }
    ]
    

    the demo on the site https://jolt-demo.appspot.com/ is :

    enter image description here