Parsing Invalid JSON - ios

because hardware limitation I need to parse invalid JSON formats. With NSJSONSerialization if the JSON is invalid or if have some special characters returns nil. For example
{
"/http/header":"
{\"code\":\"200\",
\"response\":\"The request has succeeded\",
\"body\":\H4؊捵ե7Ǫ㖮OƋ\"V鈭핬Ͱ枥ù޷+=豞EA㯕頎̵4kև΃
ΆץmZ-\"뺷뀕ԍ볰孖擽o<ҲA혃褿Уҥx蒊㟩g=Ң흨׮4YhkeȤ̪⤍βQ
䷹!긗Â㍡Զ䧡|jŔ䴕uA蝓蒎▖嗷н骭--̫Tʴͽ"}
}
This is a wrong JSON format but if we correct the format of this JSON the problem persists because we have special characters.
Gson on JAVA with this kind of JSON code don't have any problem, I can get the code and response value the only nil value is the body. There are someway to have the values?

Is it possible for you to correct the json format? Because the json values must not contain line breaks.
The valid json should look like this:
{
"/http/header":{
"code":"200",
"response":"The request has succeeded",
"body":"H4؊捵ե7Ǫ㖮OƋ\"V鈭핬Ͱ枥ù޷+=豞EA㯕頎̵4kև΃ΆץmZ-\"뺷뀕ԍ볰孖擽o<ҲA혃褿Уҥx蒊㟩g=Ң흨׮4YhkeȤ̪⤍βQ䷹!긗Â㍡Զ䧡|jŔ䴕uA蝓蒎▖嗷н骭--̫Tʴͽ"
}
}
So you can replace all line breaks and unnecessary backslashes

To check your JSON valid or invalid use : http://pro.jsonlint.com/
{
"/http/header": {
"code": "200",
"response": "The request has succeeded",
"body": "H4؊捵ե7Ǫ㖮OƋ\"V鈭핬Ͱ枥ù޷+=豞EA㯕頎̵4kև΃ΆץmZ-\"뺷뀕ԍ볰孖擽o<ҲA혃褿Уҥx蒊㟩g=Ң흨׮4YhkeȤ̪⤍βQ䷹!긗Â㍡Զ䧡|jŔ䴕uA蝓蒎▖嗷н骭--̫Tʴͽ"
}
}

Related

Graph API Unable to read JSON request payload

In power automate using Invoke an HTTP request, I am unable to make a graph API call for email sent(Post: https://graph.microsoft.com/v1.0/me/sendMail) with a customized email body (HTML elements and tags). It does not allow the below custom HTML code and errors as "Unable to read JSON request payload. Please ensure the Content-Type header is set and payload is of valid JSON format."  Please find the code below. In addition, HTML code is
Please find the code below.
And HTML Code is
This error occurs because your payload is not in a valid json format. You have to escape the quotation marks. Use \" intead of ".
{
"data": "<html><head>The title attribute example</head><body><h3 title=\"Hello HTML!\">Titled head tag example</h3></body></html>"
}
Like #Optimal already mentioned. It is good to escape the quotation marks of the HTML Body value.
However, I see the request body also has a saveToSentItems boolean property. Can you try it with that boolean as well?
Try something like below:
{
"message": {
"subject": "#{triggerBody()['text']}",
"body": {
"contentType": "HTML",
"content": "#{triggerBody()['text_1']}"
},
"toRecipients": [
{
"emailAddress": {
"address": "#{triggerBody()['text_2']}"
}
}
]
},
"saveToSentItems": "true"
}

Patch Planner Tasks Details Error - Bad request with Base-64 string Error

I am trying to call https://graph.microsoft.com/v1.0/planner/tasks/{{taskId}}/details with this request:
{
"checklist": {
"552f6163-e7d3-4e31-9015-577b0e6cc997": {
"#odata.type": "microsoft.graph.plannerChecklistItem",
"title": "Update task details",
"isChecked": false
}
}
}
I have the if-match header set properly, with the etag from the tasks. I get the below 400 Bad Request response when I run the query:
{
"error": {
"code": "",
"message": "The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters. ",
"innerError": {
"request-id": "c9781050-e409-4b88-9d7e-0a57dcec1f82",
"date": "2020-05-29T21:26:17"
}
}
}
I have tried random GUIDs, and base-64 encoded strings, to no avail. I also tried to download the Postman Environment, but I get the same error from there. Any help or guidance would be hugely appreciated.
Thanks for your time.
B.
You are getting this error because the If-Match header has an invalid value. When viewed by tools, sometimes tools include escape characters, which needs to be adjusted when making queries manually through Graph Explorer or Postman. Correct If-Match header value looks like:
W/"Base64EncodedStuff"
Aside from that issue, your query appears correct.

Rails - Rack how to TEST "invalid byte sequence in UTF-8" by sending in json

I have been working on a middleware that encodes the body for UTF-8 and returns a 422 to the client if the string cannot be encoded. I have passing specs.
There are 99,234,3978,224 articles(I googled) on how to handle this but not how to test, and by test, I want to send invalid JSON to my local server via curl or postman.
I have tried a few things all POST's
{ "name": "bad string \255" }
{ "name": "bad string \xAD" }
{ "name": "bad string <invaild japanese chars that some other post said was invaild>" }
Either ad chars where escaped the \ by adding another one or just treated it like text, which makes sense but not what I want.

Rails receive params as string

I'm building an API with Ruby on Rails. Because of that, I can't force the params to follow sone restriction.
Currently, I'm having an error with params that contained a Windows newline, because the newline isn't escaped.
The request body looks like below:
{
"name": "Jon Do",
"address": "6th Street\r\nDistrict City\r\nNation"
}
And it generated JSON::ParserError: 784: unexpected token
How to correctly process that request so params[:address] can be called without error?
When passing the data from front-end, make it JSON.stringify, and parse from back-end.
#Front-end
address: JSON.stringify(objects)
#Back-end
JSON.parse(params[:address])

JSON responses in Rails

I am using an API for my university project, which sends me multiple responses which are all valid JSON.
Both are treated as string in response.body so I need to parse them into JSON or ruby object.
The first one passes through the JSON parser but fails if I use eval which returns a ruby object
{
apiCode: "SUCCESS",
friendlyMessage: "All information saved successfully"
}
Second one passes through eval and gives me a valid Ruby object but fails as a valid JSON.
{
apiCode: "SUCCESS_WITH_ERRORS",
friendlyMessage: "Some of the information was not processed",
successfulIds: [
{
ssid: "My Test 1",
bssid: "2d:8c:e5:5c:bb:b9"
},
{
ssid: "My Test 2",
bssid: "2a:7d:a4:5c:aa:a7"
}
]
Regards,
Babar
Running both your JSON blobs through both JSON Lint and Ruby's JSON library results in an error.
The error for the first one is
Error: Parse error on line 1:
{ apiCode: "SUCCESS",
--^
Expecting 'STRING', '}', got 'undefined'
and for the second one is
Error: Parse error on line 1:
{ apiCode: "SUCCESS_WI
--^
Expecting 'STRING', '}', got 'undefined'
The errors suggest that your JSON should look like (I'm showing the first one here)
{
"apiCode": "SUCCESS",
"friendlyMessage": "All information saved successfully"
}
(which parses successfully everywhere).
Using eval for parsing data coming from some external source is a bad idea, because opens a direct code execution vulnerability.
For parsing JSON there's a build-in parser:
require 'json'
ruby_object = JSON.load(your_json_string)
For more speed, if you need it, use a dedicated json parser gem like oj directly or through MultiJson gem.
Also your second json is missing the final } so is not valid, also hash keys in json are supposed to be in "

Resources