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
Manish Gupta 122Manish Gupta 122 

JSONException: Unexpected character ('I' (code 73)): was expecting double-quote to start field name

Hi Developers,
I am facing issue when i am passing a json string and it is not getting deserialized as it is considering the whole json string as one value.
Here is my code and json string:
Code:
 public override String abc(){
        Map<String, Object> xyz = (Map<String, Object> )JSON.deserializeUntyped(jsonString);
        Account acc = new Account();
        system.debug('@@@@@'+xyz );
        for (String key : xyz.keySet()){
            String objString = (String)xyz.get(key);
            if( key == 'Account'){
                acc = (Account)JSON.deserialize(objString, Account.class);
            }
        }
jsonString:
'{"Account":"{ "Id ": "001550 ","ShippingStreet ": "abc ", "ShippingState ": "ON ", "ShippingCountry ": "A ", "ShippingCity ": "G ", "Phone ": "123456789 ", "BillingCountry ": "ABC ", "BillingCity ": "G ", "BillingStreet ": "QTR"}"}';
 
Maharajan CMaharajan C
Hi Manish,

1. Your Json is not a valid one.

Please try the below valid json:
String jsonString = '{"Account": {"Id": "001550","ShippingStreet": "abc","ShippingState": "ON","ShippingCountry": "A","ShippingCity": "G","Phone": "123456789","BillingCountry": "ABC","BillingCity": "G","BillingStreet": "QTR"}}';

2. Update in apex:

public override String abc(){
    Map<String, Object> xyz = (Map<String, Object> )JSON.deserializeUntyped(jsonString);
    Account acc = new Account();
    system.debug('@@@@@'+xyz );
    for (String key : xyz.keySet()){
    system.debug(xyz.get(key));
    String objString = JSON.serialize(xyz.get(key));
     if( key == 'Account')
    acc = (Account)JSON.deserialize(objString, Account.class);
}
}


==========================

Execute the below code in Developer console or Workbench:

String jsonstr = '{"Account": {"Id": "001550","ShippingStreet": "abc","ShippingState": "ON","ShippingCountry": "A","ShippingCity": "G","Phone": "123456789","BillingCountry": "ABC","BillingCity": "G","BillingStreet": "QTR"}}';

system.debug( ' ==> ' +  jsonstr );
Map<String, Object> xyz = (Map<String, Object> )JSON.deserializeUntyped(jsonstr );
Account acc = new Account();
system.debug('@@@@@'+xyz );
for (String key : xyz.keySet()){
    system.debug(xyz.get(key));
    String objString = JSON.serialize(xyz.get(key));
    if( key == 'Account')
    acc = (Account)JSON.deserialize(objString, Account.class);
 
}

system.debug( ' ===>  ' + acc );


Thanks,
Maharajan.C
Manish Gupta 122Manish Gupta 122
Thanks Maharajan

But now I am getting "Invalid conversion from runtime type List<ANY> to String" exception.
Maharajan CMaharajan C
Post your code where you are getting the error. When am executing my code there is no errors...