What's wrong with this JSON object? - json

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.

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" : ""}.

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"
}
}

Ruby parse string to json

So I have some json that looks like this, which I got after taking it out of some other json by doing response.body.to_json:
{\n \"access_token\": \"<some_access_token>\",\n \"token_type\": \"Bearer\",\n \"expires_in\": 3600,\n \"id_token\": \<some_token>\"\n}\n"
I want to pull out the access_token, so I do
to_return = {token: responseJson[:access_token]}
but this gives me a
TypeError: no implicit conversion of Symbol into Integer
Why? How do I get my access token out? Why are there random backslashes everywhere?
to_json doesn't parse JSON - it does the complete opposite: it turns a ruby object into a string containing the JSON representation of that object is.
It's not clear from your question what response.body is. It could be a string, or depending on your http library it might have already been parsed for you.
If the latter then
response.body["access_token"]
Will be your token, if the former then try
JSON.parse(response.body)["access_token"]
Use with double quotes when calling access_token. Like below:
to_return = {token: responseJson["access_token"]}
Or backslashes are escaped delimiters and make sure you first parse JSON.

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\"}" }