2

I am currently receving a JSON packet from an external server and placing the values in variables for further use. The JSON packet has an array that can and will grow depending on the amount of assets being sent over. I am not able to get any of the values from the array to place in any of my other variables.

@RestResource(urlMapping='/assets')

global with sharing class asset_REST_controller {

@HttpPost
global static void doPost(String jsonPayload) {

        String sf_ord_num;
        String asset_id;
        String product_code;
        String product_descrip;
        String serial_numb;                             
        String start_date_raw;
        Datetime start_date;
        List<Object> asset_info;                             


        Map<String, Object> deserialized_response = 
            (Map<String, Object>)JSON.deserializeUntyped(jsonPayload);

        sf_order_number = 
            (String)deserialized_response.get('sf_order_num');                             

        start_date_raw = 
            (String)deserialized_response.get('start_date');

        start_date = 
            (Datetime)JSON.deserialize('"' + start_date_raw + '"', DateTime.class);

        asset_info = (List<Object>)deserialized_response.get('asset_info');

    }

}

The incoming JSON packet looks like this:

jsonPayload: {"sf_ord_numb":"12345","start_date":"2020-01-15T00:00:00Z","asset_info":[{"product_code":"TEST","product_descrip":"This is a test","asset_id":"01543234321234","serial-numb":""}]}

2 Answers 2

1

You can cast each element in the List to a Map<String, Object> to continue processing.

for(Object assetObject: asset_info) {
  Map<String, Object> asset = (Map<String, Object>)assetObject;
  String product_code = (String)asset.get('product_code');
  // ... etc ... //
}

You'll probably want to write a class to hold this data, because it's a list:

public class AssetData {
  public String product_code, product_description, ...;
}

...

List<AssetData> assets = new List<AssetData>();
for(Object assetObject: asset_info) {
  Map<String, Object> asset = (Map<String, Object>)assetObject;
  AssetData data = new AssetData();
  data.product_code = (String)asset.get('product_code');
  // ... etc ... //
  assets.add(data);
}
3
  • How would I handle the data if the array expands with repeating keys? For example more than one product code coming in since there is more than one asset in the array?
    – Richard
    Dec 21, 2019 at 0:35
  • @Richard The assets list would contain each asset, including all the product codes. That's the point of the for loop.
    – sfdcfox
    Dec 21, 2019 at 5:57
  • thank you for all your help
    – Richard
    Dec 28, 2019 at 8:29
0

I would do this with a class, instead of separate variables. This is an example with a few of your variables (for reference), plus the list you want to add to:

This would be your class (could be an inner class, if you prefer). Please note that the variable names must match the JSON property names for this code to auto-deserialize.

public class JSONDetails() {
    public String sf_ord_num {get;set}
    public Date start_date {get;set}
    <more properties here>
    public List<JSONAssetInfo> asset_info {get;set;}
}

public class JSONAssetInfo() { 
    public String product_code {get;set}
    public String product_descrip {get;set}
    <more properties here>
}

Your deserializer would be much simpler, like so:

@HttpPost
global static void doPost(String jsonPayload) {
    JSONDetails details = (JSONDetails) JSON.deserialize(jsonPayload, JSONDetails.class);
}

You can then find all your variables populated with the contents of the JSON.

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.