Escaping xml encoded quotes in azure liquid mapping - json

I've been struggling for some time with the following problem. I have an xml which has to be transformed to json using liquid mapping on azure. Sometimes a node contains xml encoded double quotes, like this:
<node>
<value>"some string"and the rest</value>
</node>
my liquid mapping looks like this:
"name":"{{ node.value }}"
The result of the mapping is a following error:
{
"Code": "IncorrectLiquidTransformOutputType",
"Message": "An error occurred while converting the transformed value to JSON. The transformed value is not a valid JSON. 'After parsing a value an unexpected character was encountered: s. Path '[205].node[9].value', line 32305, position 13.'",
"Details": [
{
"Code": "IncorrectLiquidTransformOutputType",
"Message": "{\"ClassName\":\"Microsoft.Azure.Function.Common.ErrorResponses.ErrorMessageException\",\"Message\":\"An error occurred while converting the transformed value to JSON. The transformed value is not a valid JSON. 'After parsing a value an unexpected character was encountered: s. Path '[205].node[9].name', line ....., position 13.'\",\"Data\":null,\"InnerException\":{\"ClassName\":\"Newtonsoft.Json.JsonReaderException\",\"Message\":\"After parsing a value an unexpected character was encountered: s. Path '[205].node[9].value'....",
"Details": null,
"InnerError": null
}
],
"InnerError": null
}
This means that the character " is properly decoded to double quote, and then it causes problems with the json. I need to keep this character like this:
"name":"\"some string\"and the rest"
any ideas how to do it?

I got the exact same issue, but I convert the xml to json before passing it through the liquid transform. So the transformation gets a properly escaped json string:
"name":"William \"Billy\" Bob"
I tested the proposed solution above (which looks a bit weird since it replaces any occurrence with the same value), and at first it didn't do anything. However, after some fiddling around, I removed the first Replace and then it actually worked.
{{ node.value | Replace: '\"', '\"'}}

did you try to escape them in the output?
"name":"{{ node.value | Replace: '\' , '\' | Replace: '\"', '\"'}}"

Related

Escape string in a JSON object

We use Freemarker to transform one JSON to another. The input JSON is something like this:
{"k1": "a", "k2":"line1. \n line2"}
Post using the Freemarker template, the JSON is converted to:
{ \n\n "p1": "a", \n\n "p2": "line1. \n line2"}
Here is the logic we use to do the transformation
final Map<String, Object> input = JsonConverter.convertFromJson(input, Map.class);
final Template template = freeMarkerConfiguration.getTemplate("Template1.ftl");
final Writer out = new StringWriter();
template.process(input, out);
out.flush();
final String newlineFilteredResult = new JSONObject(out.toString).toString();
The conversion to JSON object fails due to a newline character inside a string for key k2 and gives the following exception:
Caused by: org.json.JSONException: Unterminated string at ...
I tried using the following but nothing works:
1. JSONObject.quote
2. JSONValue.escape
3. out.toString().replaceAll("[\n\r]+", "\\n");
I get the following exception due to the newline characters at the beginning as well:
Caused by: org.json.JSONException: Missing value at 1 [character 2 line 1]
Could someone please point me in the correct direction.
Edit
After further clarification from OP he had "${key}": "${value}" in his freemarker template and ${value} could contain line brakes. The solution in this case is to use ${value?json_string}.
Starting from FreeMarker 2.3.32 you can write "${key}": ${value?c} instead of "${key}": "${value}", because if the left-side of ?c is a string, now instead of failing, it quotes and escapes the string. Thus you don't even have to know if the left-side is a number/boolean, which must not be quoted (and ?c won't quote them), or a string, which must be quoted, as it's automatic.
Also, if the left-value is known to be missing/null sometimes, them ?cn will handle that case by printing a null literal.
Also, check out the c_format setting for best results, but by default string formatting is JSON compatible, so using ?c will be an improvement even without setting that.

Are line feeds allowed in JSON strings?

I need to send XML inside a JSON for my REST OSB 12c Proxy as follow:
{
"login": "jstein",
"identityContext": "jazn.com",
"taskId": "string",
"payload": {
"any_0": {
"any_01": "<afastamento xmlns:ns1='http: //www.tjsc.jus.br/soa/schemas/comagis/AfastamentoMagistrado' xsi:type='def: AfastamentoMagistradoType' xmlns:xsi='http: //www.w3.org/2001/XMLSchema-instance' xmlns='http: //xmlns.oracle.com/bpel/workflow/task'>
<ns1:Magistrado>719</ns1:Magistrado>
<ns1:Status>Inicial</ns1:Status>
<ns1:Vaga>8770</ns1:Vaga>
<ns1:Tipo>Licenca Nojo</ns1:Tipo>
<ns1:PeriodoReferencia/>
<ns1:DataInicialSolicitada>2015-10-10</ns1:DataInicialSolicitada>
<ns1:DataFinalSolicitada>2015-11-05</ns1:DataFinalSolicitada>
</afastamento>"
}
},
"outcome": "Start"
}
The OSB 12c send me back the error:
"errorMessage" : "ORABPEL-15235\n\nTranslation Failure.\nFailed to translate
JSON to XML. org.codehaus.jackson.JsonParseException: Illegal unquoted
character ((CTRL-CHAR, code 10)): has to be escaped using backslash to be
included in string value\n at [Source: java.io.BufferedReader#7db921c7; line:
7, column: 619]\nThe incoming data does not conform to the NXSD schema. Please correct the problem.\n"
I am testing my JSON request at JSONLint, and it always gives me the error about start a String with <:
Parse error on line 7:
"any_01": "<afastamento xmlns:
-----------^
Expecting 'STRING, 'NUMBER, 'NULL', 'TRUE', FALSE', '{', '['
No, literal line feeds (CTRL-CHAR, code 10) and newlines are control characters that are not allowed within a JSON string:
XML does not require the line feeds between elements. You can simply remove them, changing your multi-line XML document to an equivalent single-line XML document that will be able to be passed as a JSON string without problem. Or, you may want to consider escaping the line feeds \n, or more generally, escaping the entire string:
How should I escape strings in JSON? [Java]
In C# how to encode XML to output it inside JSON in the JavaScript
part of a page

JSON to DataContract with = Symbol

I am unable to convert the following JSON into DataContract Class:
{"SomeData":"Sample={"Id":-1,"Key":"test"}"}
The error shown is:
After parsing a value an unexpected character was encountered: I. Path 'SomeData', line 1, position 22."
Please let me know if it is possible to convert this JSON data into the DataContract class?
You need to use single quotes inside "Sample={ ... }":
{ "SomeData": "Sample={'Id':-1,'Key':'test'}" }
If you need them to be double quotes, you can escape them with \"
{ "SomeData": "Sample={\"Id\":-1,\"Key\":\"test\"}" }

JSON.parse file input differ from parsing string literal

Im using nodejs to parse some JSON files and insert them into mongodb,the JSON in these files have invalid JSON characters like \n,\" etc ..
The thing that i dont understand is that if i tried to parse like :
console.log(JSON.parse('{"foo":"bar\n"}'))
i get
undefined:1
{"foo":"bar
but if i tried to parse the input from the file (The file has the same string {"foo":"bar\n"})like:
new lazy(fs.createReadStream("info.json"))
.lines
.forEach(function(line){
var line = line.toString();
console.log(JSON.parse(line));
}
);
every thing works fine , i want to know if this fine and its ok to parse the files i have, or i should replace all invalid JSON characters before i parse the files ,
and why is there a difference between the two.
Thanks
If you can read "\n" if your text file, then it's not an end of line but the \ character followed by a n.
\n in a JavaScript string literal adds an end of line and they're forbidden in JSON strings.
See json.org :
To put an end of line in a JSON string, you must escape it, which means you must escape the \ in a JavaScript string so that there's "\n" in the string received by JSON.parse :
console.log(JSON.parse('{"foo":"bar\\n"}'))
This would produce an object whose foo property value would contain an end of line :

What's wrong with this JSON object?

What's wrong with this?
{ 'z': 'hello' }
Looks like a valid JavaScript dictionary to me, but both Python JSON and http://pro.jsonlint.com/ are telling me
Parse error on line 1:
{ 'z': 'hello'}
-----^
Expecting 'STRING', '}'
What am I doing wrong?
Strings must be delimited by double quotes in JSON: http://www.json.org/
They can be single quoted in Python and JavaScript, but JSON is a very small subset of JavaScript.