Problem with decode UTF character from JSON data - json

I post data from user and then convert to JSON data and store in database.
To avoid problem with escape char, I used
$jsonData = json_encode($array_json_data,JSON_HEX_APOS|JSON_HEX_QUOT);
and it converted escaped char to UXXXX char.
Now I am having problem while decoding these data.
For example how can I print quote from U0027.

use html_entity_decode to convert quotes to its actual string representation

Related

removing backslashes in mule 4 dataweave transformation

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

Change single backslash in R character string to valid JSON string

I have a string in R which escapes quotation marks:
my_text = {\"stim\":[\"platery\",\"denial\",\"generic\"]}
When using cat() I get:
{"stim":["platery","denial","generic"]}
Now my whole string is a JSON string that needs to be parsed and is evaluated invalid by JSONLint. If I copy&paste the cat() version, this is valid a JSON, so I think I just miss some pre-processing here.
I saw this SO post here, and this one, and this really good one, so I tried to replace the single quotation marks with double quotation marks for the JSON parser:
gsub("\\\\", "\\\\\\\\", my_text, fixed=TRUE)
but it did't change my string as I wanted. How can I change the string to become valid JSON?
As Wiktor said your gsub didn't work because you are attempting to replace backslashes, but there aren't any backslashes in your string. R is just using the backslashes as a way to store the double quotes. The third SO post you link does a good job explaining R's string literals which addresses this. A backslash in R is stored as a double backslash.
My first piece of advice would be to use the R package jsonlite to construct your JSON from an R object as opposed to a string if possible (heres the vignette).
Example:
myJSON <- jsonlite::toJSON(list(stim=c("platery","denial","generic")))
# {"stim":["platery","denial","generic"]}
Second, (as the third SO post again does a good job of explaining) copying/pasting the print method of the string may not be the best way to validate the JSON. I'm not sure of the use case, but R storing the double quotes with escape characters is probably not a bad thing.
If you want to get a prettier print method you can use numerous tricks in R (noquote(), cat(), print(quote=F)) but this won't change that R stores the double quotes with backslashes:
Additionally, in some cases constructing the JSON isn't necessary. I have an API built using the plumbr package that returns a list as JSON without any modifications (recJSON <- list(message=messages,recommendations=list(name=names, link=URLs)))

Decoding string of bytes back to bytes

I need to save a byte string in a json file and get it back as a bytestring.
In order to be able to dump it into the json, I had to convert the bytes to a regular string. The problem I'm having is that once I read the json and try to encode the converted bytestring, the '\' are doubled, so the strings aren't the same.
How could you do it properly? :(
Input:
salt = b'\xd5KS\xe4\x1b\xbd'
Output = b'\xd5KS\xe4\x1b\xbd'

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

What is wrong with this JSON string?

I keep getting NULL when i do $data = json_decode($data); . To encode it in JS I use var data = JSON.stringify( my_obj ); What is wrong with this JSON string?
'guide' => string (1583) "[{\"instructions\":[\"sdasda\"],\"media\":[{\"id\":95,\"titl …"
EDIT:
I am adding the entire string as requested:
[{\"instructions\":[\"sdasda\"],\"media\":[{\"id\":95,\"title\":\"item4_1444\",\"filename\":\"item4_1444.jpg\",\"url\":\"http://localhost:8888/sandbox/content/uploads/2014/08/item4_1444.jpg\",\"link\":\"http://localhost:8888/sandbox/example/change-phone-home-button/item4_1444/\",\"alt\":\"\",\"author\":\"1\",\"description\":\"\",\"caption\":\"\",\"name\":\"item4_1444\",\"status\":\"inherit\",\"uploadedTo\":86,\"date\":\"2014-08-24T16:39:15.000Z\",\"modified\":\"2014-08-24T16:39:15.000Z\",\"menuOrder\":0,\"mime\":\"image/jpeg\",\"type\":\"image\",\"subtype\":\"jpeg\",\"icon\":\"http://localhost:8888/sandbox/includes/images/media/default.png\",\"dateFormatted\":\"24/08/2014\",\"nonces\":{\"update\":\"46bbaaed37\",\"delete\":\"bcda3390c5\",\"edit\":\"ea35506ec2\"},\"editLink\":\"http://localhost:8888/sandbox/admin/post.php?post=95&action=edit\",\"sizes\":{\"thumbnail\":{\"height\":150,\"width\":150,\"url\":\"http://localhost:8888/sandbox/content/uploads/2014/08/item4_1444-150x150.jpg\",\"orientation\":\"landscape\"},\"medium\":{\"height\":300,\"width\":200,\"url\":\"http://localhost:8888/sandbox/content/uploads/2014/08/item4_1444-200x300.jpg\",\"orientation\":\"portrait\"},\"full\":{\"url\":\"http://localhost:8888/sandbox/content/uploads/2014/08/item4_1444.jpg\",\"height\":960,\"width\":640,\"orientation\":\"portrait\"}},\"height\":960,\"width\":640,\"orientation\":\"portrait\",\"compat\":{\"item\":\"\",\"meta\":\"\"},\"html_id\":\"ge-step-image-1\"}]}]
There is nothing wrong with the JSON other than the fact that the double-quotes that are supposed to delimit string values in the JSON have been encoded as if they were double-quotes within a JSON string.
For example:
\"instructions\":
Should be simply:
"instructions":
To illustrate how double-quote escaping is intended to work in JSON, consider encoding an object with a single item named height with the string value 6'2":
height: 6'2"
Encoded in JSON this would be:
{ "height": "6'2\"" }
The JSON strings are delimited with double-quotes. The double-quote internal to the string value itself is then escaped using the backslash character.
Supplementary to this of course is how you encode your string for your given source code language.
If you are using a literal string in your source which is itself double-quote delimited or places special interpretation on '\' then you must adopt the conventions required by the source language in encoding your literals.
Again, by way of an example, consider how the above string would be represented in two different languages:
// C#
s = "{ \"height\": \"6'2\\\"\" }";
// Delphi
s := '{ "height": "6''2\"" }';
In C# since '\' marks an escaped character then the '\' in the JSON must itself be escaped. The double quote characters also need to be escaped since C# string literals are also double-quote delimited.
In Delphi, string literals are single quote delimited, so there is no need to escape the double-quotes, but the single-quote within the string must be. In Delphi this involves only doubling the single quote sequence and there is no need to escape the '\' or '"' characters at all.
Hopefully this will help you understand any mistakes that you may be making in the representation of your JSON strings.