Decoding json escape sequences - json

Having created a text file with a JSON object (from an array) using json_encode, I'm now supposed to decode the same object. However, with json_decode, the unicode escape sequences don't seem to be properly converted back.
Here is the example of a string from the JSON file:
S\u00720066006f006cd industriomr\u00640065
After json_decoding, the text becomes:
Sr0066006f006cd industriomrd0065
Any idea what's going on here?

The decoding seems to work fine; the final text does indeed correspond with the encoded one. What was the object like before encoding?

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

Json : getting the unwanted result

I'm using json plugin to get the response in json.
But I m getting the unwanted result:
Here is what I get:
{"data":"[[\"service\",\"webservices\",\"document\"],[\"validation\",\"adapters\",\"server\"]]","records":25,"recordsTotal":75}
originally the data var in my action class is like this:
[["service","webservices","document"],["validation","adapters","server"]]
but json plugin adds the backslash.
The wanted result is that:
{"data":[["service","webservices","document"],["validation","adapters","server"]],"records":25,"recordsTotal":75}
Is there a way to get the later result ?
Thanks
You're representing the data as a PHP string. " is obviously a reserved character in JSON, so your serialization library is dutifully escaping the quote using /.
If you set up the PHP variable so it's an array of arrays, instead of a string representing an array of arrays, then your JSON serialization will work fine.

Converting Delphi Objects to JSON

I am using Delphi XE7 and I am having trouble converting objects into JSON. I can get some object to give back what I think is proper JSON, eg TTestObject:
{"Test":{"Field":"TestField","Operation":"TestOperation","values":
["Value1","Value2","Value3","Value4"]}}
JOBJ:= TJSONObject.Create;
JOBJ.AddPair('Test', ATestObject.JSONObj);
memo1.Lines.Add(JObj.ToJSON);
JOBJ.Free;
However, when I try to get JSON back from my objects that have properties that are objects as well, I get JSON with \ characters.
{"Exceptions":{"TestObject1":"
{\"Mode\":\"0\",\"Value\":\"100.50\",\"Days\":\"10\"}","TestObject2":"
{\"Mode\":\"0\",\"Days\":\"0\",\"UnitsSold\":\"
...
What is causing this?
The JSON is perfectly valid. Your nested objects, when represented as JSON, contain double quote characters. Since they are reserved as string delimiters they need to be escaped. Hence the use of the backslash character as the escape character.

Get Python 2.7's 'json' to not throw an exception when it encounters random byte strings

Trying to serialize a dict object into a json string using Python 2.7's json (ie: import json).
Example:
json.dumps({
'property1': 'A normal string',
'pickled_property': \u0002]qu0000U\u0012
})
The object has some byte strings in it that are "pickled" data using cPickle, so for json's purposes, they are basically random byte strings. I was using django.utils's simplejson and this worked fine. But I recently switched to Python 2.7 on google app engine and they don't seem to have simplejson available anymore.
Now that I am using json, it throws an exception when it encounters bytes that aren't part of UTF-8. The error that I'm getting is:
UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 0: invalid start byte
It would be nice if it printed out a string of the character codes like the debugging might do, ie: \u0002]q\u0000U\u001201. But I really don't much care how it handles this data just as long as it doesn't throw an exception and continues serializing the information that it does recognize.
How can I make this happen?
Thanks!
The JSON spec defines strings in terms of unicode characters. For this reason, the json module assumes that any str instance it receives holds encoded unicode text. It will try UTF-8 as its default encoding, which causes trouble when you have a string like the output of pickle.dumps which may not be a valid UTF-8 sequence.
Fortunately, fixing the issue is easy. You simply need to tell the json.dumps function what encoding to use instead of UTF-8. The following will work, even though my_bytestring is not valid UTF-8 text:
import json, cPickle as pickle
my_data = ["some data", 1, 2, 3, 4]
my_bytestring = pickle.dumps(my_data, pickle.HIGHEST_PROTOCOL)
json_data = json.dumps(my_bytestring, encoding="latin-1")
I believe that any 8-bit encoding will work in place of the latin-1 used here (just be sure to use the same one for decoding later).
When you want to unpickle the JSON encoded data, you'll need to make a call to unicode.decode, since json.loads always returns encoded strings as unicode instances. So, to get the my_data list back out of json_data above, you'd need this code:
my_unicode_data = json.loads(json_data)
my_new_bytestring = my_unicode_data.encode("latin-1") # equal to my_bytestring
my_new_data = pickle.loads(my_new_bytestring) # equal to my_data

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