removing backslashes in mule 4 dataweave transformation - json

I am fetching data from SQL Server Database and transforming it into JSON in Mule 4. My input has a single backslash and converted to double backslashes. I only need a single backslash in my output.
Input example:
abchd\kdgf
Output is:
"abchd\\kdgf"
It should be:
"abchd\kdgf"
Anyone can help with this data weave transformation?

In JSON strings the backslash character is the escape character, and has to be escaped itself to represent a single backlash. That's how JSON works, it is not a Mule issue.

Here single slash treated internally as double slash. Try the dataweave expression like below
payload replace /([\\])/ with ("")
Hope it helps

Related

How do I convince Splunk that a backslash inside a CSV field is not an escape character?

I have the following row in a CSV file that I am ingesting into a Splunk index:
"field1","field2","field3\","field4"
Excel and the default Python CSV reader both correctly parse that as 4 separate fields. Splunk does not. It seems to be treating the backslash as an escape character and interpreting field3","field4 as a single mangled field. It is my understanding that the standard escape character for double quotes inside a quoted CSV field is another double quote, according to RFC-4180:
"If double-quotes are used to enclose fields, then a double-quote appearing inside a field must be escaped by preceding it with another double quote."
Why is Splunk treating the backslash as an escape character, and is there any way to change that configuration via props.conf or any other way? I have set:
INDEXED_EXTRACTIONS = csv
KV_MODE = none
for this sourcetype in props.conf, and it is working fine for rows without backslashes in them.
UPDATE: Yeah so Splunk's CSV parsing is indeed not RFC-4180 compliant, and there's not really any workaround that I could find. In the end I changed the upstream data pipeline to output JSON instead of CSVs for ingestion by Splunk. Now it works fine. Let this be a cautionary tale if anyone stumbles across this question while trying to parse CSVs in Splunk!

Fixind invalid json in SQL

I'm parsing json in redshift using json_extract_path_text, but this json is invalid (one of the fields contains double quote inside of the string value):
"somefield": "4 *\\"`)(z"
Is there any way to get rid of this quote and replace it with some other value (I do not really care about this particular data as it is wrong anyway, but I want to fetch some other parts of this json).
It looks like you have the wrong number of backslashes in the string. You need either or 1, to just get the double quotes, or 3 to get a backslash and the double quote. But this isn't really the question.
You can use the REPLACE() function to strip the \" text out. https://docs.aws.amazon.com/redshift/latest/dg/r_REPLACE.html
REPLACE(json_text, '\\"', '')
I believe REPLACE() doesn't do any string interpretation so no additional escaping will be needed.

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)).

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

Hive and get_json_object strange behavior

I am running hive query using get_json_object to read json strings from files in HDFS.
And I bumped with some strange behavior:
if the json is as follow:
{"data":{"oneSlash":"aaa\bbb","twoSlashes":"ccc\\ddd","threeSlashes":"eee\\\fff"}}
The result of the query is:
{"oneSlash":"aaabbb","twoSlashes":"ccc\\ddd","threeSlashes":"eee\\fff"}
I understand the 'oneSlash' and the 'threeSlashes' result but why 'twoSlashes' did not equal to "ccc\ddd"?
after all '\' should be unescaped to '\'
BTW the quesry is:
SELECT get_json_object(escaping_test.data, '$.data') FROM escaping_test
it's because \b and \f is valid escape characters whereas \d is not. there's a post about this in more detail: Where can I find a list of escape characters required for my JSON ajax return type?