How to handle escape sequence's in json string in mulesoft - json

I have json input like this
{
"receivedNumber": "\357425078",
"receivedRma": "RL975F0718331133212",
"EndDate": "12/16/2017 12:54:13 PM",
"manufacturerDate": "11/14/2016",
"firstUseDate": "12/17/2016 11:58:20 AM"
}
for this input, 'json to object transformer' getting failed because of escape sequence in 'receivedNumber'
is there any solution regarding this ? how to handle escape sequence ?

try this
"{ \"receivedNumber\": \"\357425078\", \"receivedRma\": \"RL975F0718331133212\", \"EndDate\": \"12/16/2017 12:54:13 PM\", \"manufacturerDate\": \"11/14/2016\", \"firstUseDate\": \"12/17/2016 11:58:20 AM\" }";

Related

API responses adding "/" near of all " ' "

When ı print my json_dump
{"accountNumber": 4176312, "currencyCode": "USD", "ownerName": "selman", "accountType": "individual", "balance": 0.0}
But when ı return
return make_response(jsonify(json_dump), status_code)
I see this
Why this ///// is coming ? How can ı solve this ?
json_dump is, presumably, a string of JSON, (representing an object).
jsonify will convert that string to a JSON representation of that string which will cause any " in it to be escaped.
Send json_dump to the browser (where you want a JSON representation of an object).
Don't use jsonify on something that is already JSON (since a JSON representation of a JSON representation of an object isn't very useful).

org.json.JSONException: Expected literal value at character 85

This is my json string :
{
gateway=vary,
gateway_text=xyz,
gateway_data=,
start=17/02/2022 06:23:45,
end=17/03/2022 06:23:45,
promo_time=17/03/2022 06:23:45,
in_process_canceled=0.0,
id=817957632
}
When I am trying to convert it to JSON object, it is throwing the exception
val obj = JSONObject(purchaseData.toString())
Please help me resolve this.
Your json is not a valid json, remove "=" and put double quotes in property and values
{
"gateway": "vary",
"gateway_text": "xyz",
"gateway_data": "ss",
"start": "17/02/202206:23:45",
"end": "17/03/202206:23:45",
"promo_time": "17/03/202206:23:45",
"in_process_canceled": "0.0",
"id": "817957632"
}
Then it will work
This can handled by converting it to a json string ;
json = json.replace(" ","")
json = json.replace("\n","")
json = json.replace("{", "{\"")
json = json.replace("}", "\"}")
json = json.replace("=", "\":\"")
json = json.replace(",", "\",\"")
val obj = JSONObject(json)

Parsing JSON with backslash node.js

I have this JSON file,
{
"file_paths": {
"PROCESS": "C:\incoming",
"FAILED": "C:\failed"
}
}
I get an error when I try to access PROCESS or FAILED. The error is SyntaxError: Unexpected token i in JSON. It must be due to backslash. How can I access PROCESS or FAILED without editing the JSON file?
You will need to escape the backslash in your JSON String.
If you are building the JSON yourself, you can escape special characters when you build it. OR if you are not, as a post process once you have the JSON file you could do something like sed over it to replace the backslash with an escaped backslash (obviously not the ideal solution).
The reason is because the JSON is not valid do to the \ no being escaped making the reader think the i is trying to be escaped
As J Livengood said, you need to escape backslashes when inside a string. Like so:
var obj = {
"file_paths": {
"PROCESS": "C:\\incoming",
"FAILED": "C:\\failed"
}
}

Escape dots in Groovy GPath

I am using the RestAssured framework in Java, whose documentation contains this note
Note that the "json path" syntax uses Groovy's GPath notation and is not to be confused with Jayway's JsonPath syntax.
I need to validate the following JSON:
"_source": {
"logSource": {
"logger.name": "LogbackLogger",
},
}
And the selectors like
_source.logSource.logger.name or
_source.logSource.logger.name[0] return no result.
I assume this is due to the dot in the logger.name property.
If no escaping is done, logger.name is interpreted as if name was under the logger, which is not true.
How do I correctly escape the dot character in the GPath, so that logger.name is considered as a single property name?
Thanks!
You have a trivial issue.
Just wrap it in between single quote i.e., 'logger.name'
as there is special character.
Here is complete example :
def string = """{ "_source": {
"logSource": {
"logger.name": "LogbackLogger",
}
}
}"""
def json = new groovy.json.JsonSlurper().parseText(string)
println json.'_source'.logSource.'logger.name'

Escaping JSON Payload with back slash

I have an external service that returns a JSON Payload as below
{
"GetIPAResult": "{\"Data\":[{\"Name\":\"Pan1\",\"Email\":\"abc#example.com\"},{\"Name\":\"Pan2\",\"Email\":\"xyz#example.com\"}]}"
}
How could I escape the back slash? I need to use a json path and currently I cant do it. Is there a better way than String Replace? Thanks.
Regards,
Hari
Just simply parse your JSON string. like that.
var response = {
"GetIPAResult": "{\"Data\":[{\"Name\":\"Pan1\",\"Email\":\"abc#example.com\"},{\"Name\":\"Pan2\",\"Email\":\"xyz#example.com\"}]}"
};
var result = $.parseJSON(response.GetIPAResult);
Now you can get your data (in result variable) without back slash.
hari .. at a moment your json structure are as follows
{"key": "value"}
where value is a stringify of {"Data" : JSONARRAY}
here 2 case arise..
case 1:
try to change the structure of return json to
{"key": JSONOBJECT}
where JSONOBJECT -> {"DATA": JSONARRAY}
case 2:
try to convert return json to
{"key": JSONOBJECT}
where JSONOBJECT -> {"DATA": JSONARRAY}
by replace "{ with { , \" with " and }" with }
hence the result of
{
"GetIPAResult": "{\"Data\":[{\"Name\":\"Pan1\",\"Email\":\"abc#example.com\"},{\"Name\":\"Pan2\",\"Email\":\"xyz#example.com\"}]}"
}
to
{
"GetIPAResult": {"Data":[{"Name":"Pan1","Email":"abc#example.com"},{"Name":"Pan2","Email":"xyz#example.com"}]}
}