1

I need to modify my JSON. I want to remove " before [ and ]

Current Request :

{"marital-status":"Single","hospitalisation":"["hello", "world"]","ill-health":false,"specialist-condidtions":"["HEADACHE", "PANIC ATTACK"]","smoker":false,"health-concerns":false}

Modified request :

{"marital-status":"Single","hospitalisation":["hello", "world"],"ill-health":false,"specialist-condidtions":["HEADACHE", "PANIC ATTACK"],"smoker":false,"health-concerns":false}

I tried using the translate function fn:translate(req,'"[','[') but this replaced all my " as well as [.

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147

2 Answers2

0

If you have the XML in the pipeline, why don't you just use NXSD to translate automatically to json.

To do this aslong as you have the XSD For the JSON then just use a nXSD componenant the translation will happen automatically?

https://svgonugu.com/2015/12/27/using-translate-activity-for-xml-to-json/

jtyreman
  • 256
  • 3
  • 15
0

<rant>You really should fix the Java process that is generating mangled output to ensure that it generates valid JSON. Trying to repair broken data is a waste of time and defeats the purpose of using standard data formats. It also likely to fail at random times when the "fix" fails to handle all of the edge cases.</rant>

The reason why fn:translate() didn't work the way you expected is that you were probably expecting it to work more like a find/replace of the full strings, but it is character based. The first character from the second param will be replaced with the first character from the third param (or removed, if there is no corresponding character), and then each subsequent character in the strings. So, fn:translate(req,'"[','[') really meant, "replace a double quote with [ and replace [ with nothing".

In order to remove the offending characters, you could use fn:replace(), which takes a regex pattern for what to find, and a string for what to replace with:

fn:replace(fn:replace(req, '"\[', "["), '"\]', "]")
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147