1

I am attempting to deserialise a JSON response with some variables that Salesforce is having a problem handling because there is a '.' in the object name (as well as a '.' in some variable names).

Here's part of the JSON file:

{
    "gon.features": {
        "gon.app_is_ready": false,
        "show_newcomers_with_featured_field": true,
        "registration_and_onboarding_flow": true,
        "app_tracking_business_model_enabled": true,
        "product_page_enabled": true,
        "show_new_pricing_page": true,
        "change_advanced_search_path_to_markets": true,
        "publisher_portfolio_page": true,
        "app_country_split_page": true,
        "advanced_search_toggle_view": true
    }

}

And my JSON deserialise object for it:

    class cls_gon.features {
        public Boolean gon.app_is_ready;
        public boolean show_newcomers_with_featured_field;
        public boolean registration_and_onboarding_flow;
        public boolean app_tracking_business_model_enabled;
        public boolean product_page_enabled;
        public boolean show_new_pricing_page;
        public boolean change_advanced_search_path_to_markets;
        public boolean publisher_portfolio_page;
        public boolean app_country_split_page;
        public boolean advanced_search_toggle_view;
    }

The error I am getting is:

Invalid identifier: gon.features

Is there a way to handle for the variables/objects containing '.' in their name? Changing the JSON response unfortunately isn't an option...

1 Answer 1

2

You can always just deserialize into a Map<String, Object>.

Map<String, Object> data = (Map<String, Object>)JSON.deserialieUntyped(payload);
Map<String, Object> features = (Map<String, Object>)data.get('gon.features');
Boolean isReady = (Boolean)features.get('gon.app_is_ready');

If you always have the same prefix, another option would be to simply modify the payload before deserialization to remove this prefix:

String payload = payload.replace('gon.', '');

Then you can use everything after the dot as your property name:

public class Gon
{
    public Features features;
    public class Features
    {
        public Boolean app_is_ready;
        public Boolean show_newcomers_with_featured_field;
        public Boolean registration_and_onboarding_flow;
        public Boolean app_tracking_business_model_enabled;
        public Boolean product_page_enabled;
        public Boolean show_new_pricing_page;
        public Boolean change_advanced_search_path_to_markets;
        public Boolean publisher_portfolio_page;
        public Boolean app_country_split_page;
        public Boolean advanced_search_toggle_view;
    }
}
3
  • Thanks Adrian, I didn't know that this was possible. Just a quick one (I am new to this), what does payload stand for in the first line of your code? Jun 20, 2017 at 18:53
  • @KamilMieczakowski Your JSON data. The string that you specify at the top of your post. '{"gon.features": {"gon.app_is_ready": true, ...}}'
    – Adrian Larson
    Jun 20, 2017 at 18:54
  • 2
    If you're going with String.replace be careful. Are there any free text fields? If so you may want to change that to something more unique like payload.replace('"gon.app_is_ready".', '"app_is_ready"') so you don't accidentally replace the string "gon." in the data. E.g. you wouldn't want Customer owns a Ford Wagon. to become Customer owns a Ford Wa !
    – Charles T
    Jun 20, 2017 at 19:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .