function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ezra SoleymaniEzra Soleymani 

sorry, unable to parse a json: Unexpected character ('E' (code 69))

I am consuming a third party Rest Service. One of data fields is return following -1.E-02. When I pass the json data through the json  deserializer. I am hit with the following error: System.JSONException: Unexpected character ('E' (code 69)) in numeric value: Decimal point not followed by a digit at [line:1, column:1509]
 
John TowersJohn Towers
This would happen when they JSON you're trying to parse isn't formatted properly. It seems that the parser is expecting a number somewhere that you have a string. If you post the JSON you're deserializing it would be easier to identify the exact issue. Otherwise you'll have to look through the string you're parsing and make sure that it is properly formatted JSON.
Ezra SoleymaniEzra Soleymani
The Json file is valid. Here are the two fields and interger values that coming in scientific notation
  • "grossProfitDollars": 1.E-2,
  • "grossProfitPercent": 1.E-2,
John TowersJohn Towers
That's the problem. '1.E-2' isn't quoted in your JSON. If it's not quoted, the parser treats it as a number. '1.E-2' may be scientific notiation and represent a number but it's not actually a number - it's a string. So you'll either need to pass in the actual number so it's parsed as a number or quote the field so it's parsed as a string.
Ezra SoleymaniEzra Soleymani
I have created a mock json with value in quotes. I still gave me an error with josn deserializer
John TowersJohn Towers
Did it give you the same error? Or a different one?
Ezra SoleymaniEzra Soleymani
Nvm the string value work fine. the only problem is the  data from the third party cant chnage how  they send us the data. 
John TowersJohn Towers
What they're passing is not valid JSON so if possible they really should fix it. If not, you should be able to clean up the JSON string before you try to parse it. Try something like this:
 
String str = '{"grossProfitDollars": 1.E-2}';
Matcher match = Pattern.compile('[^"]([0-9]+\\.E-[0-9]+)').matcher(str);
String replacedText = match.replaceAll('"$1"');

This uses a regular expression to try to find unquoted instances of scientific notation and wrap them in quotes. Then you can just parse the 'replacedText' variable since it should be valid JSON.