Search code examples
jsonapache-nifijolt

Split string inside json into mulitple parameters


I have a JSON like this:

[
  {
    "id": "28573041|utm_source=vodafone&utm_medium=banner&utm_campaign=smartphones",
    "date": "2022-05-03"
  },
  {
    "id": "28573041|utm_campaign=Vodafone_uppers_2022",
    "date": "2022-05-03"
  }
]

I want to split these ids like:

  • before : - this is actual id;
  • before = - this is a property name for parameters;
  • after = - this is a property value;

I want to parse it and get this result:

{
  {
    "id" : "28573041",
    "date" : "2022-05-03",
    "utm_source" : "vodafone",
    "utm_medium" : "banner",
    "utm_campaign" : "smartphones"
  },
  {
    "id" : "28573041",
    "date" : "2022-05-03",
    "utm_campaign" : "Vodafone_uppers_2022"
  }
}

Parameters can be different after | and order is not guaranteed, but only possible 5 variants: -

  • utm_source
  • utm_medium
  • utm_campaign
  • utm_term
  • utm_content

Any ways to do it with JOLT or other NiFi tools?


Solution

  • Other solution in this case.

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "aux": "=split(\\|,@(1,id))",
            "id": "=firstElement(@(1,aux))",
            "newInfo": "=lastElement(@(1,aux))",
            "auxFinal": "=split(&,@(1,newInfo))"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "id": "[#2].&",
            "date": "[#2].&",
            "auxFinal": {
              "*": {
                "@": "[#4].fields[].field"
              }
            }
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "fields": {
              "*": {
                "aux": "=split(=,@(1,field))",
                "key": "=firstElement(@(1,aux))",
                "value": "=lastElement(@(1,aux))"
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "id": "[#2].&",
            "date": "[#2].&",
            "fields": {
              "*": {
                "value": "[#4].@(1,key)"
              }
            }
          }
        }
      }
    ]