Home > Mobile >  Split string inside json into mulitple parameters
Split string inside json into mulitple parameters

Time:08-11

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?

CodePudding user response:

Splitting by pipes and equality signs might be used along with distinguishin by @(..,id) qualifiers presuming that the provided id values are unique per each object such as 28573041 and 28573042 respectively

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "idd": "=split('\\|', @(1,id))",
        "id": "@(1,idd[0])",
        "idd1": "=split('&', @(1,idd[1]))"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "@(1,id).&",
        "idd1": {
          "*": "@(2,id).ide&"
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "ide*": "=split('=', @(1,&))"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&1.&",
        "ide*": {
          "0": "&2.key[]",
          "1": "&2.val[]"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "id": "&1.&",
        "date": "&1.&",
        "val": {
          "*": {
            "@": "&3.@(3,key[&])"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

the demo on the site enter image description here

CodePudding user response:

Hi You can use following operations and split it with |,&,= and then shift the relative values and remove the extra attributes :

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "id_Or": "=split('\\|',@(1,id))",
        "id": "@(1,id_Or[0])",
        "id_without_Or": "@(1,id_Or[1])",
        "id_And": "=split('&',@(1,id_without_Or))",
        "attribute1": "@(1,id_And[0])",
        "attribute11": "=split('=',@(1,attribute1))",
        "attributev1": "@(1,attribute11[1])",
        "attributev11": "@(1,attribute11[0])",
        "attribute2": "@(1,id_And[1])",
        "attribute12": "=split('=',@(1,attribute2))",
        "attributev12": "@(1,attribute12[0])",
        "attributev2": "@(1,attribute12[1])",
        "attribute3": "@(1,id_And[2])",
        "attribute13": "=split('=',@(1,attribute3))",
        "attributev13": "@(1,attribute13[0])",
        "attributev3": "@(1,attribute13[1])",
        "attribute4": "@(1,id_And[3])",
        "attribute14": "=split('=',@(1,attribute4))",
        "attributev14": "@(1,attribute14[0])",
        "attributev4": "@(1,attribute14[1])",
        "attribute5": "@(1,id_And[4])",
        "attribute15": "=split('=',@(1,attribute5))",
        "attributev5": "@(1,attribute15[1])",
        "attributev15": "@(1,attribute15[0])"
      }
    }
  }, {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[&1].&",
        "@attributev1": "[#2].@attributev11",
        "@attributev2": "[#2].@attributev12",
        "@attributev3": "[#2].@attributev13",
        "@attributev4": "[#2].@attributev14",
        "@attributev5": "[#2].@attributev15"
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "*": {
        "id_Or": "",
        "id_without_Or": "",
        "id_And": "",
        "attribute1": "",
        "attribute2": "",
        "attribute3": "",
        "attribute4": "",
        "attribute5": "",
        "attribute11": "",
        "attribute12": "",
        "attribute13": "",
        "attribute14": "",
        "attribute15": "",
        "attributev1": "",
        "attributev11": "",
        "attributev12": "",
        "attributev2": "",
        "attributev13": "",
        "attributev3": "",
        "attributev14": "",
        "attributev4": "",
        "attributev5": "",
        "attributev15": ""
      }
    }
  }
]

enter image description here

CodePudding user response:

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)"
          }
        }
      }
    }
  }
]
  • Related