+ Start a Discussion

json parse error converting string to decimal in rest api

Hi frnds,

i have a class where json parameters come in string format  among them two values come in decimal format i was not able to parse those values with my object values please help me in modfying the code and how to implement .

please find the below code and let me know waht need to be done

try {
            RestRequest req = RestContext.request;
            res = RestContext.response;
            res.statusCode = HTTP_OK;
            res.addHeader('Content-Type', CONTENT_TYPE);

                Params params = new Params(req.params);
                system.debug(' enter params'+params);
                Order__c order= createOrder(params);
                 body = upsertSObject(order);
                    catch (MyException e) {
                        if (body == null)
                             body = new ResponseBody();
                    catch (Exception e) {
                         if (res == null) throw e; // something horrible has happened!
                                res.statusCode = HTTP_ERROR;
                         if (body == null)
                                body = new ResponseBody();
                        if (body.id != null)
                            res.statusCode = HTTP_CREATED;
                            res.responseBody = Blob.valueOf(JSON.serialize(body));

     public static Order__c createOrder(Params p) {
        Order__c ord= new Order__c();
      //ord.Total_Price__c = getPrice(p.get('totalPrice',false));// not able to convert sting
        ord.Total_Quantity__c= Decimal.valueOf((p.get('totalQuatity',false))); // not able to convert the string into decimal
        ord.Buyer_Account__c = getBuyer(p.get('buyerName',false));
        ord.Order_Status__c  =p.get('status',false);
        ord.Payment_Type__c = p.get('payment',false);
        ord.Php_Id__c= p.get('phpId',false);
        ord.Seller_Account__c = getSeller(p.get('sellerName',false));
        ord.Product_Name__c = getProduct(p.get('productName',false));
         //  ord.Product_Name__c = p.get()
        return ord;
     Public static id getBuyer(String buyerName){
        Account accounts = [Select Id, Name,RecordTypeid from Account where Name =:buyerName];
        return accounts.id ;
    Public static Id getSeller(String sellerName){
        Account accounts = [Select Id, Name, RecordTypeid from Account where Name = :sellerName];
        return accounts.id ;
    Public static id getProduct(String productName){
        Product__c product = [Select Id, Name from Product__c where Name = :productName Limit 1];
        return product.id ;
    private static ResponseBody upsertSObject(SObject sObj) {
        ResponseBody body = new ResponseBody();
        try {
            if (sObj instanceof Order__c )
                upsert (Order__c ) sObj ;
            body.id = sObj.id;
        catch (Exception e) {
        return body;

    private class Params {
        Map<String, String> params {get; private set;}
        String type {get; private set;}
        Params(Map<String, String> params) {
            this.params = params;
          //  totalPrice = params.get('totalPrice');
           // type = params.get('type');
           // System.assert(typeToRT.keySet().contains(type));
        String get(String key) {
            return get(key, true);
        String get(String key, Boolean prependType) {
            String value = params.get((prependType ? type + '_' : '') + key);
            return value == null ? '' : value;

    public class ResponseBody {
        public Id id {get; set;}
        public List<ResponseError> errors {get; set;}
        public String status {get; set;}

        public ResponseBody() {
            errors = new List<ResponseError>();
            status = STATUS_OK;

        public void addError(DmlException e) {
            for (Integer i = 0; i < e.getNumDml(); i++)
                errors.add(new ResponseError(e, i));

        public void addError(Exception e) {
            status = STATUS_FAIL;
            if (e instanceof DmlException)
                addError((DmlException) e);
                errors.add(new ResponseError(e));

Hi Hareesh,

First question I have is why are you getting the body  as URL Parameters? URL Parameters are not JSON.  I think thats not a best practice to send the data in URL Parameters in the first place, and if you use Body thats when you can send it as JSON. 

In the JSON, you can send decimal as decimal, without quotes something like 


You can easily deserialize the JSOn with all the wide variety of method available in Salesforce. And you wouldnt have any issue.

Hi Karthick,

Thanks for you respone. can you provide the some sample code in for creating the body in json. please help me iam new in writting in apex rest api.
i have gone many articles from i understand how to creae the class but i could not under how to create the json sting or content need to parse .so i went this way please help me karthick