Add escape character for ctrl-char in a json string - json

I am trying to json decode a string but I am getting an error as:
"message":"error while converting json to class","level":"ERROR","level_value":40000,"stack_trace":"com.fasterxml.jackson.databind.JsonMappingException: Illegal unquoted character ((CTRL-CHAR, code 31)): has to be escaped using backslash to be included in string value\n at [Source: { \"key\": \"abc No.\u001F\u001F\u001F\u001F\u001F\u001F\u001F\u001F\u001F\u001F\u001F\u001F\u001F\u001F\u001F\u001F\u001F\u001F\u001F\u001F\u001F\u001F\u001F\u001F\u001F\u001F\u001F\u001F\u001F\u001F\u001F\u001F\u001F\u001F 106,xyz}
I am suspecting that there might be a ctrl-char character(/n) in the json. I want to know how add the extra escape character wherever this ctrl-char might be present, so that my code doesn't break while decoding the json.
Json Decode code:
public static Object jsonDecode(String s, Class c) {
Object u = null;
if(s!=null) {
try {
u = objectMapper.reader(c)
.readValue(s);
} catch (Exception e) {
logger.error("error while converting json to class", e);
}
}
return u;
}

Related

JSON decoding from stream in Kotlin

I have a server set up to send messages over a local host port. I am trying to decode the serialized json messages sent by the server and get this error.
Error decoding message: kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 55: Expected EOF after parsing, but had instead at path: $
JSON input: .....mber":13,"Timestamp":5769784} .....
The Racer State messages are formatted in JSON as follows: { “SensorId”: “value”, “RacerBibNumber” : “value”, “Timestamp” : “value” }, where the value’s are character string representations of the field values. I have also tried changing my RacerStatus Class to take String instead of Int but to a similar error. Am I missing something here? The symbol that is missing in the error was not able to be copied over so I know it's not UTF-8.
I have also added
val inputString = bytes.toString(Charsets.UTF_8)
println("Received input: $inputString")
This gets
Received input: {"SensorId":0,"RacerBibNumber":5254,"Timestamp":3000203}
with a bunch of extraneous symbols at the end.
data class RacerStatus(
var SensorId: Int,
var RacerBibNumber: Int,
var Timestamp: Int
) {
fun encode(): ByteArray {
return Json.encodeToString(serializer(), this).toByteArray()
}
companion object {
fun decode(bytes: ByteArray): RacerStatus {
print(bytes[0])
try {
val mstream = ByteArrayInputStream(bytes)
return Json.decodeFromStream<RacerStatus>(mstream)
} catch (e: SerializationException) {
println("Error decoding message: $e")
return RacerStatus(0, 0, 0)
}
// return Json.decodeFromString(serializer(), mstream.readBytes().toString())
}
}
}
So I found an answer to my question. I added a regex to include just the json components I know my json contains.
val str = bytes.toString(Charsets.UTF_8)
val re = Regex("[^A-Za-z0-9{}:,\"\"]")
return Json.decodeFromString<RacerStatus>(re.replace(str,""))
I thought that Charsets.UTF_8 would remove the misc characters but it did not. Is there a more intiuative solution? Also is there a regex that would cover all possible values in json?

How to keep escape keys from a json?

I´m making an application with dart / flutter and I need to keep the escape keys from the json server api response, how can I do that?
Example:
JSON reponse:
{"\"\"example\"\"": "value\'"}
Goal (after .fromJson and json.decode):
Class.example = "\"\"example\"\"";
Class.value = "value\'";
If you do jsonDecode(r'{"\"\"example\"\"": "value\'"}'), then you get a map with the key ""example"" and the value value' (the quotes are part of the string content here, not delimiters).
The backslashes are removed, which is to be expected because that's what the JSON specification says should happen. The escaped quotes are retained. The key does not become just example.
If you want to retain the backslashes too, then you don't actually want JSON decoding, because JSON decoding treats \" inside a string literal as introducing the character ".
If you wanted a string containing \"\"example\"\" from JSON decoding, then the original source needed to be "\\\"\\\"example\\\"\\\"".
So, if the sender of the JSON wanted you to see backslashes, they should have included and escaped those backslashes too.
If you JSON encode the string back, you get the backslashes again (at least for "), since jsonEncode('""example""') is "\"\"example\"\"".
If you really want access to the source code of the JSON input, I'd use a non-standard JSON parser, one which allows you to access the source instead of only the parsed value.
Example:
import "package:jsontool/jsontool.dart";
/// Parses [source] as JSON, but retains source escapes in strings.
Object? readJsonSourceStrings(String source) {
return _readJson(JsonReader.fromString(source));
}
Object? _readJson(JsonReader<StringSlice> reader) {
if (reader.tryObject()) {
var map = <String, Object?>{};
while (reader.hasNextKey()) {
var keySlice = reader.nextKeySource()!;
var key = keySlice.substring(1, keySlice.length - 1);
map[key] = _readJson(reader);
}
return map;
}
if (reader.tryArray()) {
var list = <Object?>[];
while (reader.hasNext()) {
list.add(_readJson(reader));
}
return list;
}
if (reader.checkString()) {
var slice = reader.expectAnyValueSource();
return slice.substring(1, slice.length - 1);
}
return reader.tryNum() ?? reader.tryBool() ??
(reader.tryNull() ? null : (throw "Unreachable"));
}
void main() {
var m = readJsonSourceStrings(r'''{"\"\"example\"\"":"value\'"}''');
print(m); // {\"\"example\"\": value\'}
}
(Disclaimer: I wrote the jsontool package.)

how to handle '\' in json string

In my json message the field which should have one '\' comes with '\\'
In the below example
"validation": "^[^\\s][a-zA-Z\\(\\) ,.-]+[^\\s]$"
Should come as
"validation": "^[^\s][a-zA-Z\(\) ,.-]+[^\s]$",
I am not getting why it is happening like this. Any one out there to help me.
"getTransactionDataRequirementsResponse": {
"return": [
{
"errorMessage": "Cannot start or end with whitespace",
"validation": "^[^\\s][a-zA-Z\\(\\) ,.-]+[^\\s]$",
},
{
"errorMessage": "Cannot start or end with whitespace",
"validation": "^[^\\s][a-zA-Z\\(\\) ,.-]+[^\\s]$",
},
}
No way I could pass an odd number of backslashes through json. Below is one test code
String jsonString = "{\"validation\" : \" 1\\ 2\\\\ 3\\\\\\ 4\\\\\\\\\ 3\\\" }";
JsonParser jsonParser = new JsonParser();
JsonElement jsonElement = jsonParser.parse(jsonString);
JsonObject jsonObject = jsonElement.getAsJsonObject();
System.out.println("validation=" + jsonObject.get("validation"));
Output comes as below
validation=" 1 2\ 3\ 4\\"
When you encode data to JSON format all special chars are escapes (e.g. '/' => '//'). And if you want to use JSON data, decodes them using the parseJSON() method.

JSON Object with Slashes in it

I am serializing to a json object using:
public static string ToJson(this object obj)
{
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
return jsonSerializer.Serialize(obj);
}
However when I'm populating a hidden field, I'm getting the slashes with it:
"[{\"ImageLink\":\"\",\"ShowOnHomePage\":null,\"Type\":\"AdListItem\",\"Key\":null,\"Title\":\"dsafdsaf\",\"Url\":\"fdsafdsa\",\"ContentSummary\":\"\u003cdiv\u003efdsafdsa\u003c/div\u003e\"},{\"ImageLink\":\"\",\"ShowOnHomePage\":null,\"Type\":\"AdListItem\",\"Key\":null,\"Title\":\"hddfg\",\"Url\":\"dsaf\",\"ContentSummary\":\"\u003cdiv\u003efdsafdsa\u003c/div\u003e\"},{\"ImageLink\":\"\",\"ShowOnHomePage\":null,\"Type\":\"AdListItem\",\"Key\":null,\"Title\":\"asfd\",\"Url\":\"asdf\",\"ContentSummary\":\"\u003cdiv\u003eafds\u003c/div\u003e\"}]"
How would I properly get rid of the \" and replace them with just " ???
Any Ideas?
Thanks.
The slashes are Javascript string escape characters.
\" -> " so you can have a quote with-in a quote.
This is true for most all C-style languages (C, C++, C#, Java, etc)

Groovy JsonSlurper JSON value with escaped quotation marks

I'm developing a small tool written in Groovy which parses JSON strings in emails. Some of these JSON strings have JSON values that contain escaped quotes.
For example:
{
"hello": "world with \"quotation marks\""
}
To parse these strings, I am using Groovy's JsonSlurper. The following code demonstrates my problem:
import groovy.json.JsonException
import groovy.json.JsonSlurper
try {
print new JsonSlurper().parseText('''
{
"hello": "world with \"quotation marks\""
}
''')
} catch (JsonException | IllegalArgumentException e) {
print e
}
See https://groovyconsole.appspot.com/script/6193189027315712 for a live demo.
When executing this code, the following exception is thrown:
groovy.json.JsonException: expecting '}' or ',' but got current char 'q' with an int value of 113
The current character read is 'q' with an int value of 113
expecting '}' or ',' but got current char 'q' with an int value of 113
line number 3
index number 35
"hello": "world with "quotation marks""
............................^
Thus, the escaping of the quotation mark is ignored by JsonSlurper. Unfortunately, I have no control over the input, i.e. the JSON strings. Therefore, I have to find a way to parse such a JSON string into a map or any other appropriate data structure.
The string has not been escaped properly in the json. The text data should be like:
'''
{
"hello": "world with \\\"quotation marks\\\""
}
'''
The string that you are getting indicates that the mail body contains the json in format:
{
"hello": "world with "quotation marks""
}
while it should be like
{
"hello": "world with \"quotation marks\""
}
If earlier is the case then you can't parse the invalid json as there is no way for code to identify about the escaped data.