Cranialsurge
Cranialsurge

Reputation: 245

Javascript encoding not valid for JSON

Are the encodings for JSON and Javascript different? I have a javascript snippet that is embedded in some JSON. However the moment a special character is present in the javascript (e.g. '(' is represented as \x28 in the JS snippet) it causes the JSON to be invalid.

In my example above, only if I update '(' to be represented as '(' instead of '\x28' does it result in valid JSON. The first representation is hex. while the second is valid HTML. I'm not familiar with the encoding types.

Could someone help me understand which encoding I need to use on the Javascript in order to ensure that it always results in the correct format for JSON?

I've included a rough example below. The special characters are '\x28rt' which denotes '(rt'. In JS it gets encoded to \x28rt. However for it to be valid JSON it needs to be ''rt

{
  "class":"myType",
  "id":"testId",
  "javascript":"function testMethod() {var v2 = function (a, b, e) {return e(a,   b);};}var v1 = 'somevalue';if (v2((v1), ('\x28rt'), function (a, b) {if (a == undefined ||  a == null) {return false;}return a === b;})) {alert('test1');};} else   {alert('test2');};}}",
"name":"Step1"
 }

Upvotes: 0

Views: 228

Answers (1)

Cheyne
Cheyne

Reputation: 11

\x28 is the ascii escaping, have you tried Unicode escaping \u0028? http://www.json.org/ says that a char string can have any Unicode character.

Upvotes: 1

Related Questions