Search code examples
jsonstringfiltertransformjolt

Jolt Spec to filter multiple value in json field


I have the following json and would like to filter few object only -

  {
    "field1": "xyz",
    "field2": "mno",
    "res1": "pqrs",
    "folder": "/folder-parent/raw/new"
  },
  {
    "field1": "xyz1",
    "field2": "mno1",
    "res1": "pqrs1",
    "folder": "/folder-parent/raw/old"
  },
  {
    "field1": "xyz1",
    "field2": "mno1",
    "res1": "pqrs1",
    "folder": "/folder-parent/changed-per-record"
  },
  {
    "field1": "xyz1",
    "field2": "mno1",
    "res1": "pqrs1",
    "folder": "/folder-parent/something-else"
  },
  {
    "field1": "xyz1",
    "field2": "mno1",
    "res1": "pqrs1",
    "folder": "/folder-parent/something/other-thing"
  }
]

I need filtered objects with /folder-parent/raw/new /folder-parent/raw/old /folder-parent/change-per-record

I tried this but does not work [ { "operation": "shift", "spec": { "*": { "folder": { "*": { "filter": "@(2, folder)=== '/folder-parent/raw/new' && @(2, folder)=== '/folder-parent/raw/old' && @(2, folder)=== '/folder-parent/change-per-record'", "": "@" } } } } } ] Please help in this case


Solution

  • Hi hope this helps you resolve your query.

    Explanation : folder : the code from folder act as an if condition if folder is /folder-parent/raw/new Or /folder-parent/raw/old Or /folder-parent/changed-per-record "@2" represent to pick values as its from 2 level above it and keep it an array.

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "folder": {
              "/folder-parent/raw/new|/folder-parent/raw/old|/folder-parent/changed-per-record": {
                "@2": "[]"
              }
            }
          }
        }
      }
    ]