2
> I have resolved the issue updating answer for my own question thanks for response Stack-exchange great help everytime :
> 
> >     1.Malformed JSON: Expected '[' 
> >     Answer:Used remove string method to remove root from response it worked.
> >     2.error 'System.JSONException: For input string: "".
> >     Answer:My json response has got parameter which is returning String and unfortunately Json2apex generated that element as integer
> > not sure why...I rectified that it is working perfect.
> 
> Hope it helps

This is Json Response:

 {"root":[{"across_na_flag":0,"added_by_date":"\/Date(1374552000000-0400)\/","added_by_user_name":611,"addr1":"Hartford Forklift Service","addr2":"50 Northern Ave","addr3":"Auburn, ME 04210-6125","addr4":" ","addr5":" ","addr6":" ","addr_sort1":"EQPDLR","addr_sort2":"WHSALE","addr_sort3":"NONSNA","address_name":"Hartford Forklift Service","address_type":0,"affiliated_cust_code":"","aging_limit_bracket":4,"alt_location_code":"1JOL","attention_email":"","attention_name":"Accounts Payable","attention_phone":2072124769,"bal_fwd_flag":0,"check_aging_limit":1,"check_credit_limit":1,"check_display_comment":"","check_extendedname_flag":0,"city":"Auburn","consolidated_invoices":0,"contact_email":"","contact_name":"Carl","contact_phone":2072124769,"country":"US","country_code":"US","credit_limit":5000,"cust_po_allow_dup":0,"cust_po_req":0,"customer_code":"CS031191","date_opened":735072,"db_credit_rating":" ","db_date":0,"db_num":" ","ddid":"","delivery_days":"","dest_zone_code":"North","dunn_message_code":"","dunning_group_id":"","entry_ordered_by":0,"extended_name":"","fin_chg_code":"N\/A","fob_code":"ORIGIN","forwarder_code":"","freight_code":"","freight_to_code":"","ftp":"","guid":"","inv_comment_code":"","invoice_copies":1,"iv_substitution":0,"late_chg_type":2,"limit_by_home":0,"location_code":"MAN","modified_by_date":"\/Date(1446818192743-0500)\/","modified_by_user_name":818,"nat_cur_code":"USD","note":"","one_cur_cust":1,"payer_soldto_rel_code":"        ","payment_code":"CHK-3RD","phone_1":2072124769,"phone_2":"","postal_code":"04210-6125","posting_code":"3RDBOS","price_code":"CNCMHCD","price_level":1,"print_stmt_flag":1,"rate_type_home":"BUY","rate_type_oper":"BUY","remit_code":"LOCKBOX","resale_num":"EXEMPT","route_code":"","route_no":0,"salesperson_code":"CORPORAT","ship_complete_flag":0,"ship_to_code":"","ship_to_history":0,"ship_via_code":"BESTWAY","short_name":"Hartford F","so_priority_code":" ","special_instr":"","state":"ME","status_type":1,"stmt_comment_code":"","stmt_cycle_code":"MNTHLYST","tax_code":"AVATAX","tax_exempt_cust_type":0,"tax_id_num":"","terms_code":"NET30","territory_code":2100,"timestamp":[0,0,0,0,156,175,60,241],"tlx_twx":"","trade_disc_percent":0,"url":"","valid_payer_flag":1,"valid_shipto_flag":1,"valid_soldto_flag":1,"vendor_code":"","writeoff_code":"OSBOS"}]}

I am trying to deserialize the Payload which is in array form facing this error'System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set'

 String jsonstring={"root":[{"across_na_flag":0,"added_by_date":"\/Date(1374552000000-0400)\/",,"addr6":" "},{........}]}
 list<Item> is my wrapper class class:
 list<Item> accountArray = (list<Item>)JSON.deserialize(jsonstring, Item[].class);

I understand it is the issue of Root here then I tried this way:

Map<String,Object> jsonMap = (Map<String, Object>)JSON.deserializeUntyped(jsonstring);
String jsonSubset = JSON.serialize(jsonMap.get('root'));
list<Item> accountArray = (list<Item>)JSON.deserialize(jsonstring, Item[].class);

I got this error 'System.JSONException: For input string: "" at [line:1, column:1356]';

I have to iterate on the response in this way to insert records:

for (Item a:accountArray){
 if(a.customer_code!=refAccount.Customer_ID__c)
               {

                 processResponse(a);                    
  }  

Wrapper class:

public class  Item {

    public List<Account> root;
    public Integer across_na_flag;
    public String added_by_date;   
    public String addr6;  


    }
  • It is perfect check on jsonlint.com – Salesforce Learner Apr 5 '16 at 8:33
  • 1
    Problem is that when you do JSON.deserializeUntyped first time, that automatically decode all the special characters (as ", \ etc), so thats why when you try to deserialize it second time - that throw you exception. I would recommend you to create one more wrapper class via json2apex or by yourself to be able to parse it directly from initial JSON like class MainWrapper{ List<Item> root; } – kurunve Apr 5 '16 at 8:34
  • Thanks for reply...I did that the wrapper which I have is from json2apex. – Salesforce Learner Apr 5 '16 at 8:43
1

Check out this post

I had the same issue, and Caspar Harmer suggested I re-encode the json at each stage and crazy as it sounds, it worked great.

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.