Why this Arduino sscanf JSON parsing problem? - json

I am trying to parse this JSON string:
{"FN":"142","SName":"stil.mp3","InPlaylist1":"1","InPlaylist2":"1","error":0}
I use this sscan statement:
' RetScanf = sscanf(OneJsonStr, "{\"FN\":\"%d\",\"SName\":\"%[^\"],\"InPlaylist1\":\"%d\",\"InPlaylist2\":\"%d\",\"error\":%d}", &Parameter1_FNo, Parameter2_FName, InP1, InP2, &err); '
but in only parses the two first parameter "FN" and "SName". any idea what I am doing wrong?
thnaks in advance
br.
Allan

In your sscanf() format, when matching the SName string, you never try to match the trailing double quote.
,\"SName\":\"%[^\"]\",
Also, since the format says that InP1/2 are integers, you should pass in their addresses (assuming they aren't already pointers).

Related

MySQL 5.7 JSON_EXTRACT does not work with quoted strings within the object: [ERROR] "Missing a closing quotation mark in string"

I am not able to extract quoted strings using JSON_EXTRACT function in MySQL 5.7.
Sample input:
{
"email": "d'souza#email.com",
"body": "I may have "random quotes '(single)/(double)" " in my source data"
}
Tried using,
SELECT
#valid_source := JSON_VALID(jsonString), NULL
IF(#valid_source, JSON_UNQUOTE(JSON_EXTRACT(jsonString, "$.email")), NULL)
I get an error stating: Invalid JSON text in argument 1 to function json_extract: "Missing a closing quotation mark in string." at position xxx
Any help will be appreciated, thanks!
Here is the fix that worked for me:
I used the operator "-->" instead of JSON_UNQUOTE(JSON_EXTRACT("jsonString")) and it did not throw me any error for any kind of quotes in my input string. Please note that the above Sample JSON was only one of the use cases that I was expecting in my input. I have around 4 million records, with all different combinations of characters and quotes since it contains the email bodies and using the operators instead of actual commands worked perfectly fine, it's weird since both are essentially the same but never the less I'm happy that I could resolve it using a small fix.
{
#valid_json := JSON_VALID(inputString),
IF(#valid_json, inputString ->> '$.email', NULL) AS EMAIL,
}
You are getting this error because the contents in the field, 'inputString' are invalid JSONs for some record. I think it's a mistake to blindly handle this error without persisting any error message for downstream / future users.
This approach may be acceptable (although I wouldn't accept this from a data engineer). A better solution would be to put a more informative error in the IF statement rather than a null, perhaps something like
CONCAT_WS(" ", 'INCORRECTLY FORMED JSON :', inputString), so that your overall function becomes
IF(valid_json(inputString), input_string ->> '$.email', CONCAT_WS(" ", 'INCORRECTLY FORMED JSON :', inputString)) as EMAIL.
That way any downstream users will be able to identify any underlying data quality issues that may be affecting the validity of conclusions. Along those lines, it may be worth having a completely separate field like
JSON_VALID(inputString) AS INPUT_JSON_VALID that you can use for easier downstream filtering.

Javascript deserializer not working for special characters

I am trying to desearilize a object as given below ,one of the string having some special characters associated with it and facing some errors.
obj= JsonConvert.DeserializeObject<response>(request.Message)
one of the input string looks like below
"Message":"{
'Id':'text me on dec may\' 17',
}"
Error details:After parsing a value an unexpected character was
encountered: 1. Path 'Id', line 4, position 56
thanks in advance
In my prior comment, I had mis-read the initial problem. Sorry about that.
In JavaScript I can do this:
JSON.parse('{"Message":"{\'Id\':\'text me on dec may\' 17\',}"}')
Which works just fine. Notice that I had to also escape the single quotes surrounding the inner string.
I have found the issue, actually it is problem with the input, after data serialization input should be appended with 2 backslash for each special characters, as when catching the data in API method, one of the backslash will be removed during data assigned(get/set) to the property.so if two slashes are there one will remove and another will use for deserailise the data.
Message":"{
'Id':'text me on dec may\' 17',
}"
thanks all for you support

Why does JSON.parse("string") fail

According to the JSON spec a string is a legitimate JSON value.
So why does this happen?
You are actually passing the bare word string in to the the function which of course is not valid JSON. To actually pass in the value "string" you need to be careful with your JavaScript.
Try this:
JSON.parse("\"string\"")
The extra pair of quotes must be escaped so they become part of the value you pass in to the function.
The Syntax error tells you: "s" is an unexpected token. A string is a valid JSON value but as the spec describes it must be enclosed in double quotes.
string
""
" chars "
Generally, you can use JSON.stringify(myValue) to check what a properly formatted JSON string of such value would be.
Because a string in JSON must be surrounded by quotation marks, and in your JSON.parse("string") call, JSON.parse never "sees" any quotation marks as part of the text it is asked to parse. The double-quotes that we see are being used to form a legal string to pass in -- they're not part of the text we're passing in.
This call works:
JSON.parse('"s"')

How To Convert JSON String That Contains Encoded Unicode

Could anyone tell me how to convert the following json object string, which contains encoded unicode characters (Chinese in this case) to human readable one using c# in asp.net?
records:[{"description":"\u849c\u8089","id":282}]
The string is submitted via Ajax from an Ext JS web application.
Any help is much appreciated.
There is no need to convert this string in any special manner. Any JSON decoder that more or less sticks to the specification will automatically create a correct string for the description attribute.
Update:
However, your current sample is not valid JSON. It's missing brackets or braces around the complete sample and it's missing double qutoes around records.
A correct JSON snippet would be:
{"records":[{"description":"\u849c\u8089","id":282}]}
Giving:
records:
[]
description: 蒜肉
id: 282
I am guessing it should be done as follows:
var bytes = Encoding.Unicode.GetBytes("<unicode string>");
// Return the Base64-encoded string.
string str = Convert.ToBase64String(b);

JSON.stringify() not escaping apostrophe

...using JSON2.js and JQUERY
as you can see from the first image the object property customerReport.Title has an apostrophe. In the code you can see that I'm calling JSON.stringify() into reportAsJson string which still has the unescaped apostrophe.
the error returned by $.ajax() is {"Message":"Invalid object passed in, \u0027:\u0027 or \u0027}\u0027 expected. ...
Initially I'm just going to ban apostrophe's from the user, but I thought JSON.stringify() handled this or do I need to set some option????
Thanks
You can avoid removing these apostrophes replacing them with an HTML entity ' - that's a single quot - and later decode HTML entities either in the client or server-side.
Following has worked for me after so many failed tries for stringify and other JSON parsing functions:
updatedString = string.replace(/('[a-zA-Z0-9\s]+\s*)'(\s*[a-zA-Z0-9\s]+')/g,"$1\\\'$2");
where
string = string having apostrophe in it.
updatedString = string with apostrophe issue resolved/escaped