Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string to array in nifi jolt

I am looking for the jolt specs to split the treefield string value to a list of JSON objects in below mentioned format.

Could someone help me with the jolt specs to convert the below mentioned input to the output

Input

[
  {
    "A": "value1",
    "B": "value2",
    "C": {
      "D": "x1",
      "E": {
        "treefield": "k1-value1#k2-value2"
      },
      "F": {
        "a": "x1",
        "x": {
          "y": 1
        }
      },
      "H": "x4"
    }
  }
]

Output

[
  {
    "A": "value1",
    "B": "value2",
    "C": {
      "D": "x1",
      "E": {
        "treefield": [
          {
            "paramid": "k1",
            "paramvalue": {
              "string": "value1"
            }
          },
          {
            "paramid": "k2",
            "paramvalue": {
              "string": "value2"
            }
          }
        ]
      },
      "F": {
        "a": "x1",
        "x": {
          "y": 1
        }
      },
      "H": "x4"
    }
  }
]
like image 297
TJJ Avatar asked May 17 '26 15:05

TJJ


1 Answers

You can apply modify-overwrite-beta transformations successively along with split function in order to seperate the concerned string by # and - characters, and then use shift transformation to shape as desired such as

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "C": {
          "E": {
            "*": "=split('#',@(1,&))"
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "C": {
          "E": {
            "*": "=split('-',@(1,&))"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&",
        "C": {
          "*": "&1.&",
          "E": {
            "*": {
              "*": {
                "0": "&4.&3.&2.[&1].paramid",
                "1": "&4.&3.&2.[&1].paramvalue.string"
              }
            }
          }
        }
      }
    }
  }
]
like image 191
Barbaros Özhan Avatar answered May 19 '26 05:05

Barbaros Özhan