You need to sign in to do that
Don't have an account?
Christos 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?
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); } }
All Answers
The JSON string is:
The class:
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;
}
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!
i am calling it in another class:
Generally when i use this parser:
I get an error of system.JSONException: Empty String
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);
}
Illegal assignment from GuaranteeJSON.Configurations to GuaranteeJSON
the whole class is now this:
i try to call the method using GuaranteeJson.parse
GuaranteeJSON.Configurations configs = GuaranteeJSON.parse(att.Body.toString());
i get the exact error:
Line: 15, Column: 1
System.JSONException: empty String at [line:1, column:2]
{"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
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.