You need to sign in to do that
Don't have an account?
RJ12
Get values in a List from JSON
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<String>.
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": "US States & Territories", "sorted": false, "urls": null }, "Id": "0Nt59000000AAAAAAA" } ] }I need help in fixing the error and also how to get valueNames in a List<String>
Error: System.JSONException: Malformed JSON: Expected '{' at the beginning of an object.
Here is what I have tried so far:
Wrapper: public class GlobalValueSetWrapper{ Metadata metadata; public class Metadata { public List<CustomValue> customValue; } public class CustomValue { public String label; public String valueName; } } Class: //calling API to get the JSON result HttpResponse res = GlobalValueSetAPIHandler.getResponse('Countries'); GlobalValueSetWrapper wrapper = (GlobalValueSetWrapper) JSON.deserialize(res.getBody(), GlobalValueSetWrapper.class);
I'm able to fix it.
All Answers
You have to use the below wrapper class for this. Just copy and paste the below code in your Org.
Better create a new wrapper class in your org and refer where you need. This wrapper class is generated by using JSON2APEX Salesforce.
In Actual GlobalValueSetAPIHandler apex class please use the below code to get the picklist values:
Thanks,
Maharajan.C
I'm able to fix it.
The JSON response you provided contains information about a global value set and its picklist values. The JSON response has a hierarchical structure, with different levels of nesting. The top-level keys in the JSON object are "size", "totalSize", "done", "queryLocator", "entityTypeName", and "records".
To deserialize this JSON response, you need to create a custom object in your code that matches the structure of the JSON. In your case, it looks like you have created a wrapper class called "GlobalValueSetWrapper" with inner classes called "Metadata" and "CustomValue".
The error message you provided indicates that there might be an issue with the JSON syntax. Specifically, it seems like the JSON response might be missing a curly brace '{' at the beginning of an object. You may want to check the JSON response to make sure it's well-formed and doesn't contain any syntax errors. https://namecombinertool.com/
To get the "valueName" fields of the picklist values in the JSON response as a List<String>, you can access the "customValue" field in the "Metadata" object of your wrapper class. Then, you can iterate over the list of "CustomValue" objects and add the "valueName" field of each object to a new List<String>.
Here's some sample code to get you started:
// Deserialize the JSON response into your custom object
GlobalValueSetWrapper wrapper = (GlobalValueSetWrapper) JSON.deserialize(res.getBody(), GlobalValueSetWrapper.class);
// Get the list of picklist values from the metadata object in your wrapper
List<GlobalValueSetWrapper.CustomValue> customValues = wrapper.metadata.customValue;
// Create a new list to hold the valueNames
List<String> valueNames = new List<String>();
// Iterate over the list of custom values and add the valueName field to the new list
for (GlobalValueSetWrapper.CustomValue customValue : customValues) {
valueNames.add(customValue.valueName);
}