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
Christos KolonisChristos Kolonis 

How to deal with white spaces in key JSON value

Hello, 

I need to create records based on a JSON. The problem right now is that the key value of the JSON contains white spaces, so the deserialization comes with null values. How can i deal with that? 
 
public class GuaranteeJSON {
    
    public List<Configurations> configurations;
    
    public class Configurations {
		public String guid;
		public String Guarantee Type;
		public String Guarantee Amount;
		public String In Advance Amount;   
}
    public static GuaranteeJSON parse(String json) {
		return (GuaranteeJSON) System.JSON.deserialize(json, GuaranteeJSON.class);
	}
}

 
Best Answer chosen by Christos Kolonis
RituSharmaRituSharma
Before parsing the JSON string, replace Guarantee Type with GuaranteeType, Guarantee Amount with GuaranteeAmount and so on. 

All Answers

Christos KolonisChristos Kolonis
Also, in the lines (public String Guarantee Type; public String Guarantee Amount; public String In Advance Amount;) i get an error.

The JSON string is:
{"configurations":[{"guid":"xxxxxxxxxxxxxx","Guarantee Type":"AutoCalculation","Guarantee Amount":"50","In Advance Amount":""}]}

 
RituSharmaRituSharma
Before parsing the JSON string, replace Guarantee Type with GuaranteeType, Guarantee Amount with GuaranteeAmount and so on. 
This was selected as the best answer
Christos KolonisChristos Kolonis
I tried to do so, but i have the errors on:

User-added image

The class:
public class GuaranteeJSON {
    
    public List<Configurations> configurations;
    
    public class Configurations {
		public String guid;
		public String Guarantee Type;
		public String Guarantee Amount;
		public String In Advance Amount;   
}
    public static GuaranteeJSON parse(String json) {
		return (GuaranteeJSON) System.JSON.deserialize(json.replace('Guarantee Amount', 'GuaranteeAmount'), GuaranteeJSON.class);
	}
}

 
RituSharmaRituSharma
The way you have replaced Guarantee Amount with GuaranteeAmount, please replace other variables too.

Even below lines should be changed:

public class Configurations {
public String guid;
public String Guarantee Type;
public String Guarantee Amount;
public String In Advance Amount;
}

To:

public class Configurations {
public String guid;
public String GuaranteeType;
public String GuaranteeAmount;
public String In AdvanceAmount;
}
Christos KolonisChristos Kolonis
Don't know how to use replace for more values. Can you help pls? Newbie here. Thanks for helping!
RituSharmaRituSharma
You may do like this:

String jsonStr = json.replaceAll('Guarantee Amount', 'GuaranteeAmount');
jsonStr = json.replaceAll('Guarantee Type', 'GuaranteeType');
jsonStr = json.replaceAll('Advance Amount', 'AdvanceAmount');
return (GuaranteeJSON) System.JSON.deserialize(jsonStr, GuaranteeJSON.class);
 
Christos KolonisChristos Kolonis
Thank you very much! However i get null values, do you have any ideas why is this happening?
Christos KolonisChristos Kolonis
I have changed a little bit the class as the objects records were of type double:
 
public class GuaranteeJSON {
    
    public List<Configurations> configurations;
    
    public class Configurations {
		public String guid;
		public String GuaranteeType;
		public Double GuaranteeAmount;
		public Double InAdvanceAmount;   
}
    public static GuaranteeJSON parse(String json) {
        String jsonStr = json.replaceAll('Guarantee Amount', 'GuaranteeAmount');
		jsonStr = json.replaceAll('Guarantee Type', 'GuaranteeType');
		jsonStr = json.replaceAll('Advance Amount', 'AdvanceAmount');
		return (GuaranteeJSON) System.JSON.deserialize(jsonStr, GuaranteeJSON.class);
	}
    
    
}

The deserialization gives me the guid but the rest are null values. Any thoughts? Thank you!

User-added image
RituSharmaRituSharma
What's the JSON? How are you calling the parse method?
Christos KolonisChristos Kolonis
The JSON string is:
 
{"configurations":[{"guid":"xxxxxxxxxxxxxx","Guarantee Type":"AutoCalculation","Guarantee Amount":"50","In Advance Amount":""}]}

i am calling it in another class:
 
public class GuaranteeCreator {
    
    public static list <Guarantee__c> createGuarantee(){
        
        Attachment att = [select id, body from Attachment where name like '%guarantee%' limit 1];
        system.debug(att);
		system.debug(att.Body);
		system.debug(att.Body.toString());
		
        
        GuaranteeJSON deserializedResults = new GuaranteeJSON();
        deserializedResults = el_GuaranteeJSON.parse(att.Body.toString());
        system.debug('DESERIALIZED JSON: ' + deserializedResults);


 
RituSharmaRituSharma
Print the value of att.Body.toString() to see what value are you getting.
Christos KolonisChristos Kolonis
i am getting this value:

User-added image
Generally when i use this parser:
 
public static GuaranteeJSON parse(String json) {
        String jsonStr = json.replaceAll('Guarantee Amount', 'GuaranteeAmount');
		jsonStr = json.replaceAll('Guarantee Type', 'GuaranteeType');
		jsonStr = json.replaceAll('In Advance Amount', 'InAdvanceAmount');
		return (GuaranteeJSON) System.JSON.deserialize(jsonStr, GuaranteeJSON.class);
	}
I get an error of system.JSONException: Empty String
RituSharmaRituSharma
Change code of parse method as below:

public static Configurations parse(String json) {
        String jsonStr = json.replaceAll('Guarantee Amount', 'GuaranteeAmount');
        jsonStr = jsonStr.replaceAll('Guarantee Type', 'GuaranteeType');
        jsonStr = jsonStr.replaceAll('Advance Amount', 'AdvanceAmount');
        jsonStr = jsonStr.replace('{"configurations":[', '');
        jsonStr = jsonStr.replace(']}', '');
        
        return (Configurations) System.JSON.deserialize(jsonStr, Configurations.class);
    }
Christos KolonisChristos Kolonis
I changed the method but when i try to call an error occurs:
Illegal assignment from GuaranteeJSON.Configurations to GuaranteeJSON

the whole class is now this:
 
public class GuaranteeJSON {
    
    public List<Configurations> configurations;
    
    public class Configurations {
        public String guid;
		public String GuaranteeType;
		public Double GuaranteeAmount;
		public Double InAdvanceAmount;
   
}
    public static Configurations parse(String json) {
        
        String jsonStr = json.replaceAll('Guarantee Amount', 'GuaranteeAmount');
        jsonStr = jsonStr.replaceAll('Guarantee Type', 'GuaranteeType');
        jsonStr = jsonStr.replaceAll('In Advance Amount', 'InAdvanceAmount');
        jsonStr = jsonStr.replace('{"configurations":[', '');
        jsonStr = jsonStr.replace(']}', '');
        
        return (Configurations) System.JSON.deserialize(jsonStr, Configurations.class);
    }
        
    
}

i try to call the method using GuaranteeJson.parse
RituSharmaRituSharma
You will need to call the method like below:

GuaranteeJSON.Configurations configs = GuaranteeJSON.parse(att.Body.toString());
Christos KolonisChristos Kolonis
I get again the error of empty String: System.JSONException: empty String
RituSharmaRituSharma
At what line are you getting the error?
Christos KolonisChristos Kolonis
i am executing in anonymous window:
 
Attachment att = [select id, body from Attachment where name like '%guarantee%' limit 1];
        system.debug(att);
		system.debug(att.Body);
		system.debug(att.Body.toString());


el_GuaranteeJSON.Configurations configs = el_GuaranteeJSON.parse(att.Body.toString());
system.debug(configs);

i get the exact error:


Line: 15, Column: 1
System.JSONException: empty String at [line:1, column:2]
RituSharmaRituSharma
As per your previous message, json string is as below:

{"configurations":[{"guid":"xxxxxxxxxxxxxx","Guarantee Type":"AutoCalculation","Guarantee Amount":"50","In Advance Amount":""}]}

Value of In Advance Amount is an empty string but in your class, it has been declared as Double. Two ways to solve this:
1. Instead of declaraing it as double, declare as string and then use that string value(if not null) to get the double value
2. Source JSON should have 0 as the value instead of empty string
Christos KolonisChristos Kolonis
Ok understood! like this?
 
public class GuaranteeJSON {
    
    public List<Configurations> configurations;
    
    public class Configurations {
        public String guid;
		public String GuaranteeType;
		public Double GuaranteeAmount;
		public String InAdvanceAmount;
        Double myDouble = Double.valueOf(InAdvanceAmount);

}

 
RituSharmaRituSharma
In your class, you declare variable as String. And where you need to use them, convert to double.

 public class Configurations {
        public String guid;
        public String GuaranteeType;
        public String GuaranteeAmount;
        public String InAdvanceAmountStr;   
}

Please mark my original answer as the best answer if that was helpful.