Are line feeds allowed in JSON strings? - json

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

Related

Escaped HTML string in JSON invalid

I have a JSON with HTML code. I escaped all characters using this tool. When validating the JSON I get
Parse error on line 2:
{ "html": "<table class=\"MsoT
-----------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'
When entered into postmans body I can see that the string is valid until certain point:
Full json: here
I do not see any problem with the string. What's wrong with it?
I suspect a problem with the numerous double quotes within the HTML string since the basic JSON structure is {"key" : "value"}. Maybe you want to change your double quotes within the HTML string to to single quotes or some other character leaving something like {"html" : ""}.

Escaping xml encoded quotes in azure liquid mapping

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: '\"', '\"'}}"

Parsing JSON with backslash node.js

I have this JSON file,
{
"file_paths": {
"PROCESS": "C:\incoming",
"FAILED": "C:\failed"
}
}
I get an error when I try to access PROCESS or FAILED. The error is SyntaxError: Unexpected token i in JSON. It must be due to backslash. How can I access PROCESS or FAILED without editing the JSON file?
You will need to escape the backslash in your JSON String.
If you are building the JSON yourself, you can escape special characters when you build it. OR if you are not, as a post process once you have the JSON file you could do something like sed over it to replace the backslash with an escaped backslash (obviously not the ideal solution).
The reason is because the JSON is not valid do to the \ no being escaped making the reader think the i is trying to be escaped
As J Livengood said, you need to escape backslashes when inside a string. Like so:
var obj = {
"file_paths": {
"PROCESS": "C:\\incoming",
"FAILED": "C:\\failed"
}
}

How to pass a JSON string to API Gateway as query Parameter

How can i pass a JSON object like {val: 1} to my Lambda function as a query parameter?
Following Standardized way to serialize JSON to query string? i URL-encoded my JSON object and requested the following: mysite.com/path?json=%7B%22val%22%3A%201%7D
As requestTemplates i tried the following two options
"json": "$input.params().querystring.json"
"json": "$util.parseJson($input.params().querystring.json)"
But i got this error message:
{"message": "Could not parse request body into json: Unexpected
character (\'v\' (code 118)): was expecting comma to separate OBJECT
entries\n at [Source: [B#37a2970e; line: 1, column: 47]"}
If i do not encode the query string so: mysite.com/path?json={"val":1} i get a 400 error
Your mapping template is producing no valid JSON, you have to wrap the key/value pair in curly braces
I guess you do not want to wrap the value in quotes, it will be a string and no object otherwise
You can use $util.urlDecode to decode URL encoded strings
Your mapping template should look like this:
{"json": $util.urlDecode($input.params().querystring.json)}
For mysite.com/path?json=%7B%22val%22%3A%201%7D this mapping template will result in the following JSON:
{
"json": {
"val": 1
}
}
If you want the querystring JSON to be passed on the root level to your Lambda function use this as a mapping template:
$util.urlDecode($input.params().querystring.json)

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.