Search code examples
xmlxquerymarklogic

Additional backslash during XML to json conversion in XQuery Marklogic


I'm working with XML to JSON conversion in XQuery for the content with special characters.

XML : <text>$$\parent* \$$</text>

Using json:transform-to-json() with custom config, getting the below result

Actual result: { "text": "$$\\parent* \\$$" }

Expected result : {"text": "$$\parent* \$$"}

I tried multiple ways like using xdmp:quote() and xdmp:to-json() methods but not giving the expected result.

Is there a way to restrict the addition of extra backslash during conversion in XQuery?


Solution

  • {"text": "$$\parent* \$$"} is not valid JSON.

    In JSON the \ needs to be escaped as \\.

    You can verify that what is currently being produced is valid, and what you are expecting to be validated is invalid by validating them on https://jsonlint.com

    Don't confuse what you see in the JSON value with the data that it represents. The value $$\parent* \$$ in JSON is $$\\parent* \\$$, but when you parse/read and ask for the value of text using APIs, it will return $$\parent* \$$.