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" : ""}.
Related
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: '\"', '\"'}}"
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.
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
I'm attempting to read the following JSON file ("my_file.json") into R, which contains the following:
[{"id":"484","comment":"They call me "Bruce""}]
using the jsonlite package (0.9.12), the following fails:
library(jsonlite)
fromJSON(readLines('~/my_file.json'))
receiving an error:
"Error in parseJSON(txt) : lexical error: invalid char in json text.
84","comment":"They call me "Bruce""}]
(right here) ------^"
Here is the output from R escaping of the file:
readLines('~/my_file.json')
"[{\"id\":\"484\",\"comment\":\"They call me \"Bruce\"\"}]"
Removing the quotes around "Bruce" solves the problem, as in:
my_file.json
[{"id":"484","comment":"They call me Bruce"}]
But what is the issue with the escapement?
In R strings literals can be defined using single or double quotes.
e.g.
s1 <- 'hello'
s2 <- "world"
Of course, if you want to include double quotes inside a string literal defined using double quotes you need to escape (using backslash) the inner quotes, otherwise the R code parser won't be able to detect the end of the string correctly (the same holds for single quote).
e.g.
s1 <- "Hello, my name is \"John\""
If you print (using cat¹) this string on the console, or you write this string on a file you will get the actual "face" of the string, not the R literal representation, that is :
> cat("Hello, my name is \"John\"")
Hello, my name is "John"
The json parser, reads the actual "face" of the string, so, in your case json reads :
[{"id":"484","comment":"They call me "Bruce""}]
not (the R literal representation) :
"[{\"id\":\"484\",\"comment\":\"They call me \"Bruce\"\"}]"
That being said, also the json parser needs double-quotes escaping when you have quotes inside strings.
Hence, your string should be modified in this way :
[{"id":"484","comment":"They call me \"Bruce\""}]
If you simply modify your file by adding the backslashes you will be perfectly able to read the json.
Note that the corresponding R literal representation of that string would be :
"[{\"id\":\"484\",\"comment\":\"They call me \\\"Bruce\\\"\"}]"
in fact, this works :
> fromJSON("[{\"id\":\"484\",\"comment\":\"They call me \\\"Bruce\\\"\"}]")
id comment
1 484 They call me "Bruce"
¹
the default R print function (invoked also when you simply press ENTER on a value) returns the corresponding R string literal. If you want to print the actual string, you need to use print(quote=F,stringToPrint), or cat function.
EDIT (on #EngrStudent comment on the possibility to automatize quotes escaping) :
Json parser cannot do quotes escaping automatically.
I mean, try to put yourself in the computer's shoes and image you should parse this (unescaped) string as json: { "foo1" : " : "foo2" : "foo3" }
I see at least three possible escaping giving a valid json:
{ "foo1" : " : \"foo2\" : \"foo3" }
{ "foo1\" : " : "foo2\" : \"foo3" }
{ "foo1\" : \" : \"foo2" : "foo3" }
As you can see from this small example, escaping is really necessary to avoid ambiguities.
Maybe, if the string you want to escape has a really particular structure where you can recognize (without uncertainty) the double-quotes needing to be escaped, you can create your own automatic escaping procedure, but you need to start from scratch, because there's nothing built-in.
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.