0

I'm doing a REST API call to get the global value set and trying to deserialize the JSON result to get the picklist values in a List.

Here is the JSON:

 {
  "size": 1,
  "totalSize": 1,
  "done": true,
  "queryLocator": null,
  "entityTypeName": "GlobalValueSet",
  "records": [
    {
      "attributes": {
        "type": "GlobalValueSet",
        "url": "/services/data/v53.0/tooling/sobjects/GlobalValueSet/0Nt59000000AAAAAAA"
      },
      "Metadata": {
        "customValue": [
          {
            "color": null,
            "default": false,
            "description": null,
            "isActive": null,
            "label": "USA",
            "urls": null,
            "valueName": "USA"
          },
          {
            "color": null,
            "default": false,
            "description": null,
            "isActive": null,
            "label": "Canada",
            "urls": null,
            "valueName": "Canada"
          }
        ],
        "description": null,
        "masterLabel": "Countries",
        "sorted": false,
        "urls": null
      },
      "Id": "0Nt59000000AAAAAAA"
    }
  ]
}

I need help in fixing the error and also how to get valueNames in a List

Error: System.JSONException: Malformed JSON: Expected '{' at the beginning of an object.

Here is what I have tried so far:

Wrapper:

Here is my original wrapper that covers all blocks but still throwing the same error.    

public class GlobalValueSetWrapper{
    public Integer size;
    public Integer totalSize;
    public boolean done;
    public QueryLocator queryLocator;
    public String entityTypeName;
    public List<Records> records;

    class QueryLocator {
    }

    class Records {
        public Attributes attributes;
        public Metadata Metadata;
        public String Id;
    }
    
    class Attributes {
        public String type; 
        public String url;  
    }
    
    class Metadata {
        public List<CustomValue> customValue;
        public Description description;
        public String masterLabel;
        public boolean sorted;
        public URLs urls;
    }

    class CustomValue {
        public Color color;
        public boolean default;
        public Description description;
        public IsActive isActive;
        public String label;
        public URLs urls;
        public String valueName;    
    }

    class Color {
    }
    class Description {
    }
    class IsActive {
    }
    class URLs {
    }

    public static GlobalValueSetWrapper parse(String json){
        return (GlobalValueSetWrapper) System.JSON.deserialize(json, GlobalValueSetWrapper.class);
    }

Class:

//calling API to get the JSON result
HttpResponse res = GlobalValueSetAPIHandler.getResponse('Countries');
GlobalValueSetWrapper wrapper = (GlobalValueSetWrapper) JSON.deserialize(res.getBody(), GlobalValueSetWrapper.class);
1
  • Have you confirmed your res.getBody() is the JSON you expect? Is it a string that starts with a {?
    – Nick Cook
    Sep 22 at 5:14
0

The reason is, your wrapper is only written for the parsing of metadata block. However, there is another block ahead of the metadata block.

A sample route would be current JSON like below - object.records[0].Metadata then your logic will work.

You can easily generate wrappers using https://json2apex.herokuapp.com/, those will work without hiccups.

Or if you need only metadata block, then you have to write other processing code to get that block as a string using either Maps or other means.

2
  • I tried by adding other blocks too but it gave the same error. SO I trimmed the wrapper to my requirement. Posted my original wrapper below.
    – RJ12
    Sep 22 at 5:15
  • Updated my Question with original wrapper. When I tried 'object.records[0].Metadata' it is saying "Type is not visible: GlobalValueSetWrapper.Metadata"
    – RJ12
    Sep 22 at 5:52
0

I'm able to fix this.

I forgot to give public to the classes.

Solution to get Global Picklist values from HttpResponse:

public class GlobalValueSetWrapper{
    public List<Records> records;

    public class Records {
        public Metadata metadata;
    }
    
    public class Metadata {
        public List<CustomValue> customValue;
    }

    public class CustomValue {
        public String label;
        public String valueName;    
    }

    public static List<String> getPicklistValues(HttpResponse response){
        List<String> picklistValues = new List<String>();
        
        GlobalValueSetWrapper wrapper = (GlobalValueSetWrapper) System.JSON.deserialize(response.getBody(), GlobalValueSetWrapper.class);
        
        for(GlobalValueSetWrapper.customValue value : wrapper.records[0].metadata.customValue){
            picklistValues.add(value.valueName);
            system.debug('Piclist Value: ' + value.valueName);
        }
        system.debug('Piclist Values: ' + picklistValues);
        
        return picklistValues;
    }
}

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.