Search code examples
jsonapache-nifijolt

Apache Nifi processor to update JSON data before returning into HandleHttpResponse


My Apache processor setup is HandleHttpRequest -> InvokeHTTP ->HandleHttpResponse.

I'm hitting an API from Postman which invokes another API and gets the JSON data and returns.

Now in between InvokeHTTP -> HandleHttpResponse, I'm trying to add another processor which modifies data before returning a response.

My JSON is from InvokeHTTP

{
   "id": 1,
   "menuTempId": 28,
   "conceptId": 252,
   "menuId": 1,
   "currency": "SAR",
   "language": "En",
   "updatedAt": 1695114353000,
   "countryId": "SA",
   "version": "v1",
   "categories": [
      {
         "position": 5,
         "id": 367,
         "name": "Our New",
         "isHidden": 0,
         "is_toggle": 0,
         "products": [
            {
               "id": 2724,
               "position": 3,
               "name": "Electric Lemonade",
               "services": null,
               "inSide": 0
            },
            {
               "id": 2725,
               "position": 4,
               "name": "Electric Lemonade 2",
               "services": null,
               "inSide": 0
            }
         ]
      },
      {
         "position": 4,
         "id": 368,
         "name": "Our New 2",
         "isHidden": 0,
         "is_toggle": 0,
         "products": [
            {
               "id": 2726,
               "position": 3,
               "name": "Electric Lemonade3",
               "services": null,
               "inSide": 0
            },
            {
               "id": 2727,
               "position": 4,
               "name": "Electric Lemonade 4",
               "services": null,
               "inSide": 0
            }
         ]
      }
   ]
}

I want to modify it to look like this:

{
   "id": 1,
   "currency": "SAR",
   "language": "En",
   "countryId": "SA",
   "version": "v1",
   "categories": [
      {
         "position": 4,
         "id": 368,
         "name": "Our New 2",
         "products": [
            {
               "id": 2726,
               "position": 3,
               "name": "Electric Lemonade3"
            },
            {
               "id": 2727,
               "position": 4,
               "name": "Electric Lemonade 4"
            }
         ]
      }
   ]
}

I want to remove a few keys from JSON data. I have tried using UpdateAttribute -> JoltTransformJSON, but it's not working.

Can you help me understand from which processor and configuration I can achieve this scenario.


Solution

  • Only adding a JoltTransformJSON processor along with the following specification

    [
      {
        "operation": "shift",
        "spec": {
          "id|currency|language|countryId|version": "&"
        }
      }
    ]
    

    is enough.

    Where the pipe operators are used similar to they're(or double pipes are) used in some well-known programming languages as OR operators and the ampersand stands for replicating the values of those keys.

    Edit : Yes, you can get the latest desired output as well. Use the transformation below due to the need of arbitrarily chosen object among objects of the products array such as

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "categories": "=lastElement"
        }
      },
      {
        "operation": "shift",
        "spec": {
          "id|currency|language|countryId|version": "&",
          "categories": {
            "position|id|name": "&1[0].&",
            "products": {
              "*": {
                "id|position|name": "&3[0].&2[&1].&"
              }
            }
          }
        }
      }
    ]