Search code examples
jsontextreplaceapache-nifijolt

Nifi - Replace values


Good Afternoon!

I have a JSON with:

{
  "cnpjemitente" : "48791685000168",
  "pedido" : "543306",
  "pedidocliente" : { },
  "emissao" : "20220912"
}

I need to replace the value "pedidocliente: {}" to:

{
  "cnpjemitente" : "48791685000168",
  "pedido" : "543306",
  "pedidocliente" : null,
  "emissao" : "20220912"
}

Sometimes the value will come in the field, I just want to send null when it is empty with '{}'.

How can I do it this way?

Thanks!


Solution

  • You can use a modify-overwrite-beta transformation spec within a JoltTransformJSON processor such as

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "pedidocliente": null
        }
      }
    ]
    

    as you only need to change an individual attribute's value without affecting the others.

    If it's the case that the value does not return always {}(an empty object), then rather use a shift transformation spec such as

    [
      {
        "operation": "shift",
        "spec": {
          "pedidocliente": {
            "*": "&1.&"
          },
          "*": "&"
        }
      }
    ]