Search code examples
jsonjolt

Filter according the content of a field


I have the following input json:

INPUT

{
  "temp": "1010328|1010329",
  "data": [
    {
      "product": {
        "items": [
          {
            "item_number": "1010328",
            "attributes": [
              {
                "id": "myId01",
                "type": "int"
              }
            ]
          },
          {
            "item_number": "1010329",
            "attributes": [
              {
                "id": "myId02",
                "type": "int"
              }
            ]
          },
          {
            "item_number": "1010330",
            "attributes": [
              {
                "id": "myId03",
                "type": "int"
              }
            ]
          }
        ]
      }
    }
  ]
}

within the items array , the items should be filtered according to the value of "temp":

DESIRED OUTPUT

{
  "temp": "1010328|1010329",
  "data": [
    {
      "product": {
        "items": [
          {
            "item_number": "1010328",
            "attributes": [
              {
                "id": "myId01",
                "type": "int"
              }
            ]
          },
          {
            "item_number": "1010329",
            "attributes": [
              {
                "id": "myId02",
                "type": "int"
              }
            ]
          }
         
        ]
      }
    }
  ]
}

I tried to do a normal filtering in jolt with the string 1010328|1010329 assigned to a nifi variable "temp". But apparently jolt does not accept the value of variables, when it comes to "filter-tasks".


Solution

  • You can start with converting "temp" attribute to an array, loop under the indexes of the array and reform back to its original structure as in the following transformation :

    [
      { //convert "temp" attribute to an array with the identical name
        "operation": "modify-overwrite-beta",
        "spec": {
          "temp": "=split('\\|',@(1,&))"
        }
      },
      {
        "operation": "shift",
        "spec": {
          "temp": "&",
          "data": {
            "*": {
              "product": {
                "items": {
                  "*": {
                    "*": "@(1,item_number).&5.&4.&3.&2.&" //keep the all nestine key names
                  }
                }
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "temp": {
            "*": {
              "*": {
                "@(3,&)": ""
              }
            },
            "@": "&" //keep the "temp" array
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "temp": "&",
            "*": {
              "*": {
                "*": {
                  "*": "&3[&2].&1.&[]"
                }
              }
            }
          }
        }
      },
      { //convert "temp" back to an attribute
        "operation": "modify-overwrite-beta",
        "spec": {
          "temp": "=join('|',@(1,&))"
        }
      }
    ]
    

    the demo on the site Jolt Transform Demo Using v0.1.1 is ( just tweaked the "temp" ) :

    enter image description here