JSON array is unable to transmit a String having backslash - json

i am tranmitting a message via JSONArray but getting reponse code 500.
reason is that My Message String contains a character '\'.
String s="apples $54\KG"

Backslash is an escape character. Use \\ instead. There is a JSON.stringify function too that might help.

back slash charahter is not supproted by GSM.

Related

Why is a JSON array of URLs returning as invalid?

This is my JSON that is returning as "Invalid JSON":
["http://4x4forever.com", "http://www.4xfools.com", "http://www.4play-4wheelers.com/", "http://www.ci4wi.org/portal2/", "http://www.erieshoresjeeps.com/", "http://firewalker4x4.org/", "http://www.wegotmud.com/", "http://ironhorse4x4s.com/", "http://www.kifourwheelers.com/forum/"]
I imagine it has something to do with the slashes and double slashes. But even when I escape them like this, it still doesn't work:
[\"http:\/\/4x4forever.com\", \"http:\/\/www.4xfools.com\", \"http:\/\/www.4play-4wheelers.com\/\", \"http:\/\/www.ci4wi.org\/portal2\/\", \"http:\/\/www.erieshoresjeeps.com\/\", \"http:\/\/firewalker4x4.org\/\", \"http:\/\/www.wegotmud.com\/\", \"http:\/\/ironhorse4x4s.com\/\", \"http:\/\/www.kifourwheelers.com\/forum\/\"]
Do slashes and double slashes need to be escaped? I read in this answer that JSON enables you to escape forward slashes but it's not necessary.
The error is coming because you are escaping "
Leave " as it is it will work
["http:\/\/4x4forever.com", "http:\/\/www.4xfools.com", "http:\/\/www.4play-4wheelers.com\/", "http:\/\/www.ci4wi.org\/portal2\/", "http:\/\/www.erieshoresjeeps.com\/", "http:\/\/firewalker4x4.org\/", "http:\/\/www.wegotmud.com\/", "http:\/\/ironhorse4x4s.com\/", "http:\/\/www.kifourwheelers.com\/forum\/"]

Strip backslashes from encoded JSON response

Building a Json respose with erlang. First I construct the data in terms and then use jsx to convert it to JSON:
Response = jsx:term_to_json(MealsListResponse),
The response actually is valid JSON according to the validators I have used:
The problem is when parsing the response in the front end. Is there a way to strip the backslashes from the Erlang side, so that the will not appear on the payload response?
The backslashes are not actually part of the string. They're just used when the string is printed as a term - that is, in the same way you'd write it in an Erlang source file. This works in the same way as character escapes in strings in C and similar languages: inside double quotes, double quotes that should be part of the string need to be escaped with backslashes, but the backslashes don't actually make it into the string.
To print the string without character escapes, you can use the ~s directive of io:format:
io:format("~s~n", [Response]).
If you're sending the response over a TCP socket, all you need to do is converting the string to binary with an appropriate Unicode conversion. Most of the time you'll want UTF-8, which you can get with:
gen_tcp:send(MySocket, unicode:characters_to_binary(Response)).

How does parsing special characters in JSON work?

Alright, I know i’m probably going to get yelled at for asking such a ‘simple’ question (seems to be the trend around here) but check it out…
I am building a JSON parser and got everything working correctly except the parsers ability to deal with special characters. I am trying to implement the same special characters that are listed on http://www.json.org/ namely, [", \, /, b, f, n, r, t, u].
When I run these special characters through the builtin JSON.parse method though, most of them either return an error or don’t do anything at all
JSON.parse('["\\"]')
SyntaxError: Unexpected end of input
JSON.parse("['\"']")
SyntaxError: Unexpected token ‘
JSON.parse('["\b"]')
SyntaxError: Unexpected token
JSON.parse('["\f"]')
SyntaxError: Unexpected token
Yes, I see the other post "Parsing JSON with special characters", it has nothing to do with my question. Don't refer me to another question, I have seen them all. How does parsing special characters in JSON work?
JSON.parse expects JavaScript strings.
JavaScript string literals use backslashes for escaping.
JSON uses backslashes for escaping.
So...
JSON.parse('["\\\\"]')
// ["\"]
JSON.parse("['\"']")
// SyntaxError: Unexpected token '
// JSON doesn't have single quotes as string delimiters!
JSON.parse('["\\b"]')
// [""]
JSON.parse('["\\f"]')
// [""]
JSON.parse('["\\\\b"]')
// ["\b"]
JSON.parse('["\\\\f"]')
// ["\f"]
The reason you've got an issue here is because \ is also a marker for special characters within javascript strings.
Take your first example: '["\\"]'. As javascript parses this string, \\ is escaped to a single \ in your string, so the value passed to JSON.parse method is actually ["\"] - hence the "unexpected end of input error".
Essentially, you need to cater for the javascript parser by doubling-up on the backslash escape sequences. In this case, to pass your intended ["\\"] value to JSON.parse, you need to use JSON.parse('["\\\\"]') in javascript as that will pass the string ["\\"] into the JSON.parse method.

How do I retain backslashes in strings when using JSON.stringify?

So I got a string that has a backslash in it. "kIurhgFBOzDW5il89\/lB1ZQnmmY=".
I tried adding an extra '\', but JSON.stringify( "kIurhgFBOzDW5il89\\/lB1ZQnmmY=") returns the string with two backslashes instead of one. Is there any way to keep the backslash using JSON.stringify?
JSON.stringify doesn't remove the backslash, it encodes it. When you use JSON.parse on the other end, or whatever you do to decode your JSON, it will return the original string.
The backslash is escaping the forward slash. So JSON.stringify("\/") returns "/" since it sees an escaped forward slash, so its just a forward slash. JSON.stringify("\\/") sees a backslash being escaped, and then a forward slash next to that, so it returns "\/". You cannot preserve the "exact" string when you stringify, since parsing a json string will not escape characters, so you get back your original data, just unescaped.
JSON.parse(JSON.stringify("kIurhgFBOzDW5il89\\/lB1ZQnmmY="))
// "kIurhgFBOzDW5il89\/lB1ZQnmmY="

The url in json contains backslashes

I was just wondering why my wcf rest returns json which contains backslahses in the url. it is as below:
https:\/\/s3.amazonaws.com\/reiaustralia\/1fc00dfab25044ecb31e4882121b535e\/jpg\/download.jpg?AWSAccessKeyId=AKIAISTDESL6TBRAVM4Q&Expires=1380692091&Signature=MduuaUAjQRisadtM%2FDuVDemexLY%3D
Thanks
Forward slashes can be escaped with a backslash in JSON, but they don't have to be. So either one of the following:
{"url":"http://www.example.com/"}
or
{"url":"http\/\/www.example.com\/"}
Will parse into an object which has a url property whose string value is http://www.example.com/.
Some technologies will escape out the slashes when generating JSON, and some won't. PHP, for example, has an option called JSON_UNESCAPED_SLASHES which lets you control whether or not to escape your slashes.
You can get see the various escape characters from the json.org home page in the "string" section.
Because // (double slash) in javascript means comment and /{string}/ (string inside slash) is mean regula expression.
So. To keep correct value in json it have to put \ (back slash) in front of / (slash).
They are just escape characters, and when u consume the string in your application it would just be fine