Swift Json Single Quote Parse - json

I have a problem while parsing json which includes single quote. I am using JSONDecoder. I added response from API at below and I don't want to do any replacement or some regex operations. Are there any workaround for that?
"{\'value1\': true, \'value2\':\'2021-02-08\'}"

Your string is simply not valid JSON. Since it's not valid JSON there's no way to configure JSONDecoder to decode it.
If you aren't in charge of the service that outputs that string, the only thing you can do to use it with JSONDecoder is to modify the string to become valid JSON by, as you suggested, doing text replacement.

Related

Convert inconsistently formatted JSON String to Object

I'm having the below JSON coming in as a String input to my code. Since the string isn't uniformly formatted, overcoming the escape characters and grouping of the quotes to read the string and convert it into Java Object and sub-objects has run into issues
{"payload":{"details":"{\"source\":\"incor\",\"type\":\"build\",\"created\":\"1553855543108\",\"organization\":null,\"project\":null,\"application\":null,\"_content_id\":null,\"attributes\":null,\"requestHeaders\":{}}","content":"{\"project\":{\"name\":\"spinner\",\"lastBuild\":{\"building\":false,\"number\":0}},\"master\":\"IncorHealthCheck\"}","rawContent":null,"eventId":"bb357b79-069b-426d-8d21-8d04b06f5009"},"eventName":"city_spinner_events"}
I've tried using GSON, Jackson so far to try and read the String and convert into object and sub-objects. However, I've been able to objectify only the top level object. I face issues while I need to create sub-objects due to the escape characters and misreading of grouping of quotes by the parser. It throws errors and exceptions.
The expected JSON is as below which can be converted to object :
{"payload":{"details":{"source":"incor","type":"build","created":"1553855543108","organization":null,"project":null,"application":null,"_content_id":null,"attributes":null,"requestHeaders":{}},"content":{"project":{"name":"spinner","lastBuild":{"building":false,"number":0}},"master":"IncorHealthCheck"},"rawContent":null,"eventId":"bb357b79-069b-426d-8d21-8d04b06f5009"},"eventName":"city_spinner_events"}
Try unescapeJava from org.apache.commons.text.StringEscapeUtils,
StringEscapeUtils.unescapeJava(str);

Raw string field value in JSON file

In my JSON file, one of the fields has to carry the content of another file (a string).
The string has CRLFs, single/double quotes, tabs.
Is there a way to consider my whole string as a raw string so I don't have to escape special characters?
Is there an equivalent in JSON to the string raw delimiter in C++?
In C++, I would just put the whole file content inside : R"( ... )"
Put simply, no there isn't. Depending on what parser you use, it may have a feature that allows this and/or there may be a variant of JSON that allows this (examples of variants include JSONP and JSON-C, though I'm not aware of one specifically that allows for the features you are looking for), but the JSON standard ubiquitous on the web does not support multiline strings or unescaped special characters.
A workaround for the lack of raw string support in JSON is to Base64 encode your string before adding it to your JSON.

Why to use NSData to Json

Every time I make a call to Json, I need to use NSData and Json.deserialize.
Since Json is just a string, why cannot I just read it through rest and then use it at the other side, without using NSData, Json.Serialization, etc ?
JSON is a method for sending data through strings (not binary). Using as a string is possible, but you'd have to parse it yourself:
Here's a JSON string:
"{\"test\": \"foo\"}"
and it can be converted to
{test: "foo"}
and used like a Hash/Dictionary.
I mean, you can't get the use a string as a dictionary, can you?
"Nope.".get("Nope")
JSON is not a string. JSON is a binary format. That binary format is human readable but it's not a string. That's why we have to use NSData and why we have tools for JSON encoding and decoding instead of creating JSON data by appending strings together.

Json object with backslash, gson serialization

I want to convert a java object to json object with string format*. I am using gson library. Is there any way to do that.
(I am not sure if it is the correct name for this structure) json object with string format:
*
{ [\"name\":\"Ajay\",\"age\":30,\"email\":\"ajay#ajay.com\"]}
I'm pretty sure gson itself can't handle this, but you can. Given string s looking like
{ [\"name\":\"Ajay\",\"age\":30,\"email\":\"ajay#ajay.com\"]}
you only need to call gson on s.replace("\\\"", "\""). Simply clean up you string, so it looks like it should (your quotation marks look differently, maybe you need to fix it, too).
This may be too abstruse, but if you create a JSON string (through GSON or another library like JSON.org) and GSON-serialize that string, you will get the backslashes. This has been an irritation for me, but it would work for your case, with more code than the replace but safer if backslashes are otherwise valid in your JSON.

Does HTML need to be encoded when passed back in JSON?

When passing HTML back through a response in JSON format, does it need to get encoded?
Yes. You would pass the HTML code in a string, so any quotation marks and backslashes in the code would have to be encoded.
Example:
<div onclick="alert('Line 1\nLine2');">show</div>
would be encoded into a string like this:
"<div onclick=\"alert('Line 1\\nLine2');\">show</div>"
and for example put in a JSON object representation like this:
{"html":"<div onclick=\"alert('Line 1\\nLine2');\">show</div>"}
Simple answer "no" JSON does not need to be encoded when passed back in JSON. JSON object should be DIRECTLY parsable by a javascript engine. Check the following:
JSON Standard
JSON Lint