1

I am trying get the device_config field from a deserialized JSON packet and cast it as a List<Object> I keep getting the following error Invalid conversion from runtime type String to List<ANY>. If I try to cast it as a String I get the following error Invalid conversion from runtime type List<ANY> to String. The code below is incomplete with what I ultimately want to do with it. Really need to get over this hump. Please help

@RestResource(urlMapping='/order/assets')        

global with sharing class REST_asset_controller {

    @HttpPost
    global static void doPost(String JSON_packet) {


        System.debug('JSON_packet: ' + JSON_packet);

        Map<String, Object> deserialized_JSON;

        String order_number;
        String account_id;
        String account_name;

        List<Object> shipment_details;
        Map<String, Object> shipment_info;

        String product_code;
        String product_short_name;
        String quantity_shipped;

        List<Object> device_list;

        deserialized_JSON = (Map<String, Object>)JSON.deserializeUntyped(JSON_packet);
        order_number = (String)deserialized_JSON.get('order_number');

        account_id = (String)deserialized_JSON.get('account_id');
        account_name = (String)deserialized_JSON.get('account_name');

        shipment_details = (List<Object>)deserialized_JSON.get('shipment_details');

        List<Asset> assets = new List<Asset>();

        System.debug('JSON_packet: ' + JSON_packet);
        System.debug('deserialized_JSON:' + deserialized_JSON);
        System.debug('salesforce_order_number: ' + order_number);
        System.debug('salesforce_account_id: ' + account_id);
        System.debug('salesforce_account_name: ' + account_name);
        System.debug('shipment_details: ' + shipment_details);

        for(Object shipment : shipment_details) {


            shipment_info = (Map<String, Object>)shipment;
            Asset assets_list = new Asset();

            device_list = (List<Object>)shipment_info.get('device_list');

            System.debug('shipment_info: ' + shipment_info);
            System.debug('device_list: ' + device_list);

            assets_list.order_number__c = order_number;


            for(Object device : device_list) {

                System.debug('device: ' + device);

                assets_list.device__c = (String)device;


                //List<Object>device_config = (List<Object>)shipment_info.get('device_config');
                String device_config = (String) shipment_info.get('device_config');


                System.debug('device_config: ' + device_config);

            }


            assets.add(assets_list);
            System.debug('assets_list: ' + assets_list);

        }

        Database.insert(assets, False);

    }


}

Below is an example of the JSON packet:

{
    "order_number": "",
    "sf_order_number": "10000001",
    "account_id": "0000001",
    "account_name": "Test account",
    "shipment_details": [
        {
            "product_code": "22-5578",
            "product_short_name": "TestProd1",
            "qty_shipped": "3",
            "device_list": [
                "000000000100000",
                "000000000100001",
                "000000000100002"
            ],
            "device_config": ""
        },
        {
            "product_code": "22-5571",
            "product_short_name": "TestProd2",
            "qty_shipped": "5",
            "device_list": [
                "000000000200001",
                "000000000200002",
                "000000000200003",
                "000000000200004",
                "000000000200005"
            ],
            "device_config": ""
        },
        {
            "product_code": "22-5571",
            "product_short_name": "TestProd2",
            "qty_shipped": "5",
            "device_list": [
                "000000000300002",
                "000000000300003",
                "000000000300004",
                "000000000300005",
                "000000000300006"
            ],
            "device_config": [
                "Test sensor 1",
                "Test sensor 2",
                "Test sensor 3",
                "Test sensor 4 (Wireless)"
            ]
        }
    ]
}
5
  • 1
    Please include an example payload or this question will be virtually impossible to answer.
    – Adrian Larson
    Commented Mar 3, 2020 at 22:20
  • Basically, any of these types of calls are likely to be causing it: deserialized_JSON = (Map<String, Object>)JSON.deserializeUntyped(JSON_packet); - your JSON is likely not in the right format - it might be a JSON Array perhaps. Commented Mar 3, 2020 at 22:29
  • In your sample JSON, device_config seems to be an array and that you are trying to cast it to string would throw the exception which you are getting.
    – Jayant Das
    Commented Mar 3, 2020 at 22:41
  • I am able to deserialize this payload without issue into Map<String, Object>. Seems like perhaps that is not the actual complete content of what you are trying to pass to deserializeUntyped.
    – Adrian Larson
    Commented Mar 3, 2020 at 23:12
  • @AdrianLarson I have updated the JSON packet to more closely relate whats being passed through
    – Richard
    Commented Mar 5, 2020 at 16:28

1 Answer 1

0

The formatting with the JSON packet being transmitted to me seems to be the problem. The parameter device_config switches from being a string, device_config : "", to an array of strings, device_config : [...], whenever it isn't sent with the [] which is confusing Salesforce. Thank you all for your help

You must log in to answer this question.

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