2

I am having trouble with deserialising a jsonstring. In my debug I get the error System.JSONException: Unknown field: MyJSON.number. As number is a reserved field in salesforce I replace it with numberRep. I dont get why I get this error.

webservice

  @HttpPost
     global static void createOpportunity() {
      RestRequest req = RestContext.request;  
         string a = req.requestBody.toString().remove('\\');
         system.debug('DEB1: '+ a);         
          MyJSON tClass = MyJSON.parse(a);
         system.debug('DEB2: '+ tClass);
         system.debug('DEB3: '+ tClass.billing.address_1 );
         system.debug('DEB4: '+ tClass.cart_hash);
    }

Class

public class MyJSON{
    public Integer id;  //160
    public Integer parent_id;   //0
    public String numberRep;    //160
    public String order_key;    //wc_order_mGsqFiG3RfTGz
    public String created_via;  //admin
    public String version;  //3.5.7
    public String status;   //pending
    public String currencyRep;  //EUR
    public String date_created; //2019-03-22T07:29:08
    public String date_created_gmt; //2019-03-22T07:29:08
    public String date_modified;    //2019-03-22T07:29:14
    public String date_modified_gmt;    //2019-03-22T07:29:14
    public String discount_total;   //0.00
    public String discount_tax; //0.00
    public String shipping_total;   //0.00
    public String shipping_tax; //0.00
    public String cart_tax; //0.00
    public String total;    //0.00
    public String total_tax;    //0.00
    public boolean prices_include_tax;
    public Integer customer_id; //0
    public String customer_ip_address;  //
    public String customer_user_agent;  //
    public String customer_note;    //
    public cls_billing billing;
    public cls_shipping shipping;
    public String payment_method;   //
    public String payment_method_title; //
    public String transaction_id;   //
    public cls_date_paid date_paid;
    public cls_date_paid_gmt date_paid_gmt;
    public cls_date_completed date_completed;
    public cls_date_completed_gmt date_completed_gmt;
    public String cart_hash;    //
    public cls_meta_data[] meta_data;
    public cls_line_items[] line_items;
    public cls_tax_lines[] tax_lines;
    public cls_shipping_lines[] shipping_lines;
    public cls_fee_lines[] fee_lines;
    public cls_coupon_lines[] coupon_lines;
    public cls_refunds[] refunds;
    public class cls_billing {
        public String first_name;   //
        public String last_name;    //
        public String company;  //
        public String address_1;    //
        public String address_2;    //
        public String city; //
        public String state;    //
        public String postcode; //
        public String country;  //
        public String email;    //
        public String phone;    //
    }
    public class cls_shipping {
        public String first_name;   //
        public String last_name;    //
        public String company;  //
        public String address_1;    //
        public String address_2;    //
        public String city; //
        public String state;    //
        public String postcode; //
        public String country;  //
    }
    public class cls_date_paid {
    }
    public class cls_date_paid_gmt {
    }
    public class cls_date_completed {
    }
    public class cls_date_completed_gmt {
    }
    public class cls_meta_data {
    }
    public class cls_line_items {
    }
    public class cls_tax_lines {
    }
    public class cls_shipping_lines {
    }
    public class cls_fee_lines {
    }
    public class cls_coupon_lines {
    }
    public class cls_refunds {
    }
    public static MyJSON parse(String json){
        String jsonReplace =  json.replace('"currency":', '"currencyRep":');
               jsonReplace =  jsonReplace.replace('"number"','"numberRep"');
        return (MyJSON) System.JSON.deserializeStrict(json, MyJSON.class);
    }
}

Jsonstring

{"id":164,"parent_id":0,"number":"164","order_key":"wc_order_UeaNTHik3jec4","created_via":"admin","version":"3.5.7","status":"completed","currency":"EUR","date_created":"2019-03-22T08:37:18","date_created_gmt":"2019-03-22T08:37:18","date_modified":"2019-03-22T08:37:32","date_modified_gmt":"2019-03-22T08:37:32","discount_total":"0.00","discount_tax":"0.00","shipping_total":"0.00","shipping_tax":"0.00","cart_tax":"0.00","total":"0.00","total_tax":"0.00","prices_include_tax":false,"customer_id":0,"customer_ip_address":"","customer_user_agent":"","customer_note":"","billing":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":"","email":"","phone":""},"shipping":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":""},"payment_method":"","payment_method_title":"","transaction_id":"","date_paid":"2019-03-22","date_paid_gmt":"2019-03-22T08:37:32","date_completed":"2019-03-22T08:37:32","date_completed_gmt":"2019-03-22T08:37:32","cart_hash":"","meta_data":[],"line_items":[],"tax_lines":[],"shipping_lines":[],"fee_lines":[],"coupon_lines":[],"refunds":[]}
  • N.B. Consider the convention currencyX and numberX instead of currencyRep, and numberRep – cropredy Mar 23 at 18:36
3

Your class passes the wrong string to JSON.deserialize().

public static MyJSON parse(String json){
    String jsonReplace =  json.replace('"currency":', '"currencyRep":');
           jsonReplace =  jsonReplace.replace('"number"','"numberRep"');
    return (MyJSON) System.JSON.deserializeStrict(json, MyJSON.class);

You're transforming the string, but then not using the transformed version. Change the final line to

    return (MyJSON) System.JSON.deserializeStrict(jsonReplace, MyJSON.class);
  • Kicking myself right now for not seeing this. – Derek F Mar 23 at 14:49
  • @DavidReed Thanks! I feel really bad for missing this one. – Thomas Mar 23 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.