Search code examples
apache-nifi

Extract and modify text from JSON (Apache Ni-Fi)


I'm using InvokeHttp to get json as response from REST API. My json looks like:

{
    "resourceNames": [
        "customers/123",
        "customers/12345",
        "customers/555",
        "customers/9890"
    ]
}

How can i extract from this json 4 strings without customers prefix?

123
12345
555
9890

Solution

  • You can use JoltTransformJSON -> PutDatabaseRecord using the following JOLT spec:

    [
      {
        "operation": "shift",
        "spec": {
          "resourceNames": {
            "*": {
              "customers/*": {
                "$(0,1)": "[].id"
              }
            }
          }
        }
      }
    ]
    

    That will create the following output:

    [ {
      "id" : "123"
    }, {
      "id" : "12345"
    }, {
      "id" : "555"
    }, {
      "id" : "9890"
    } ]
    

    which you can then send to PutDatabaseRecord and it will insert a row for each JSON object in your flow file (assuming you're using a JsonTreeReader in PutDatabaseRecord) with the given value into the id column.