Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

NiFi Replace Text: how t0 replace string " [{" with "{”

avatar

i have the following son structure and want to replace the special character string "[{" and "]}" with "{" and "}" :

{"result": {"step": 1800, "start_time": 1491174000, "end_time": 1491260400, "curves": [{"color": "#a05830", "rrddata": [0.603695, 1.06903, 0.94504, 0.68786, 31.3228, 0.316447, 0.808407, 0.247655, 0.174552, 0.123072, 0.62, 0.0689, 0.30758, 0.0869783, 0.14478, 0.305993, 0.808873, 0.193055, 0.113133, 0.46116, 8.047, 1.88388, 2.62721, 0.770247, 8.06144, 2.25591, 22.3061, 57.5539, 0.270233, 1.50602, 0.819887, 5.90425, 0.43361, 0.526907, 2.46678, 0.759873, 0.451133, 0.25843, 0.224033, 0.661373, 1.1279, 0.348587, 0.277142, 0.06647, 0.16693, 0.06225, 0.0588483, 0.08057], "line_type": "area", "title": "Disk utilization"}]}, "result_code": 0}

1 ACCEPTED SOLUTION

avatar
Master Guru

The following configuration of ReplaceText should work for you:

16496-replacetext-1-element-array.png

where the Search Value is the following:

[\[\]](\{|\})

This matches [{ or ]} (and also [} and ]{ which shouldn't show up if your input is valid JSON) and replaces it with whichever curly brace it found. Note that this is a fairly specific solution, where the array is the last element of an object (i.e. the end pattern is not the end-of-object followed by end-of-array, rather the reverse).

A more forgiving solution (for your input JSON) might be to use the following Chain spec in a JoltTransformJSON processor:

[
  {
    "operation": "shift",
    "spec": {
      "result": {
        "curves": {
          "*": {
            "@": "result.curves"
          }
        },
        "*": "result.&"
      },
      "*": "&"
    }
  }
]

This "hoists" the object in the 1-element array up one level, achieving the same result as the ReplaceText pattern above.

View solution in original post

3 REPLIES 3

avatar
Master Guru

The following configuration of ReplaceText should work for you:

16496-replacetext-1-element-array.png

where the Search Value is the following:

[\[\]](\{|\})

This matches [{ or ]} (and also [} and ]{ which shouldn't show up if your input is valid JSON) and replaces it with whichever curly brace it found. Note that this is a fairly specific solution, where the array is the last element of an object (i.e. the end pattern is not the end-of-object followed by end-of-array, rather the reverse).

A more forgiving solution (for your input JSON) might be to use the following Chain spec in a JoltTransformJSON processor:

[
  {
    "operation": "shift",
    "spec": {
      "result": {
        "curves": {
          "*": {
            "@": "result.curves"
          }
        },
        "*": "result.&"
      },
      "*": "&"
    }
  }
]

This "hoists" the object in the 1-element array up one level, achieving the same result as the ReplaceText pattern above.

avatar
Rising Star

How to perform the same for the very first occurrence of [ and last occurrence of ].

avatar

Thanks Matt!

Works perfekt ...