1

When trying to parse a JSON response we can only see one child value in the whole response even though five are included in the response.

This is the code that is being used to parse the JSON:

try {

    //Execute web service call here     
    HTTPResponse res = http.send(req);
    String resp = res.getBody();
    String s = resp.substring(1, resp.length() - 1);

    fromJSON r = fromJSON.parse();
    System.debug('r: '+r);
    Map<Integer, Integer> myMap = new Map<Integer, Integer>();
    for(fromJSON.StockLevels stocklevels: r.stocklevels) {
        myMap.put(stocklevels.StockLevel, stocklevels.Available);

    }
    System.debug('MAP: '+ myMap);

    //Helpful debug messages
    System.debug(res.toString());
    System.debug('STATUS:' + res.getStatus());
    System.debug('STATUS_CODE:' + res.getStatusCode());
    System.debug('Response:' + res.getBody());

} catch (System.CalloutException e) {
    //Exception handling goes here....
}

Here is the class that holds the variables:

public class fromJSON{
    public String StockItemId;
    public cls_Suppliers[] Suppliers;
    public StockLevels[] stockLevels;
    public cls_ItemChannelDescriptions[] ItemChannelDescriptions;
    public cls_ItemExtendedProperties[] ItemExtendedProperties;
    public cls_ItemChannelTitles[] ItemChannelTitles;
    public cls_ItemChannelPrices[] ItemChannelPrices;
    public cls_Images[] Images;
    public String ItemNumber;   //TEST_SS7050
    public String ItemTitle;    //TEST_Swan Stabilo Boss Highlighter Yellow
    public String BarcodeNumber;    //4006381118740
    public String MetaData; //Stabilo Boss Highlighter Yellow. The original highlighter since 1972. Available in a choice of attractive fluorescent colors with a long cap off time. The distinctive design stops the pen from rolling away and makes it usable anywhere. Brand quality from Schwann Stabilo, the inventor of the highlighter.
    public boolean isBatchedStockType;
    public Double PurchasePrice;    //0
    public cls_RetailPrice RetailPrice;
    public Double TaxRate;  //0
    public String PostalServiceId;  //00000000-0000-0000-0000-000000000000
    public cls_PostalServiceName PostalServiceName;
    public String CategoryId;   //b4e35a83-0e63-4e85-995f-5a99ce998fcf
    public String CategoryName; //Linnworks Test Category
    public String PackageGroupId;   //00000000-0000-0000-0000-000000000000
    public cls_PackageGroupName PackageGroupName;
    public Double Height;   //0
    public Double Width;    //0
    public Double Depth;    //0
    public Double Weight;   //0.1
    public String CreationDate; //2018-01-02T09:21:36.163
    public Integer InventoryTrackingType;   //0
    public boolean BatchNumberScanRequired;
    public boolean SerialNumberScanRequired;
    //public String StockItemId;  //c3566ffc-9a7a-4de1-97eb-77d250a7bf25

    public class StockLevels {
        public cls_Location Location;
        public Integer StockLevel;  //100
        public Double StockValue;   //0
        public Integer MinimumLevel;    //20
        public Integer InOrderBook; //2
        public Integer Due; //0
        public Integer InOrders;    //2
        public Integer Available;   //98
        public Double UnitCost; //0
        public String SKU;
        public boolean AutoAdjust;
        public String LastUpdateDate;   //2018-01-02T09:21:35
        public String LastUpdateOperation;  //OPENING STOCK
        public String rowid;    //00000000-0000-0000-0000-000000000000
        public boolean PendingUpdate;
        public String StockItemId;  //c3566ffc-9a7a-4de1-97eb-77d250a7bf25
    }

    class cls_Suppliers {
    }

    class cls_Location {
        public String StockLocationId;  //00000000-0000-0000-0000-000000000000
        public String LocationName; //Default
        public boolean IsFulfillmentCenter;
        public cls_LocationTag LocationTag;
        public String BinRack;  //C01-0000
    }

    class cls_LocationTag {
    }

    class cls_SKU {
    }

    class cls_ItemChannelDescriptions {
    }

    class cls_ItemExtendedProperties {
    }

    class cls_ItemChannelTitles {
    }

    class cls_ItemChannelPrices {
    }

    class cls_Images {
    }

    class cls_RetailPrice {
    }

    class cls_PostalServiceName {
    }

    class cls_PackageGroupName {
    }

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

Here is the JSON response string:

{"Suppliers":[],"StockLevels":[{"Location":{"StockLocationId":"00000000-0000-0000-0000-000000000000","LocationName":"Default","IsFulfillmentCenter":false,"LocationTag":null,"BinRack":"C01-0000"},"StockLevel":100,"StockValue":0.0,"MinimumLevel":20,"InOrderBook":2,"Due":0,"InOrders":2,"Available":98,"UnitCost":0.0,"SKU":null,"AutoAdjust":false,"LastUpdateDate":"2018-01-02T09:21:35","LastUpdateOperation":"OPENING STOCK","rowid":"00000000-0000-0000-0000-000000000000","PendingUpdate":false,"StockItemId":"c3566ffc-9a7a-4de1-97eb-77d250a7bf25"}],"ItemChannelDescriptions":[],"ItemExtendedProperties":[],"ItemChannelTitles":[],"ItemChannelPrices":[],"Images":[],"ItemNumber":"TEST_SS7050","ItemTitle":"TEST_Swan Stabilo Boss Highlighter Yellow","BarcodeNumber":"4006381118740","MetaData":"Stabilo Boss Highlighter Yellow. The original highlighter since 1972. Available in a choice of attractive fluorescent colors with a long cap off time. The distinctive design stops the pen from rolling away and makes it usable anywhere. Brand quality from Schwann Stabilo, the inventor of the highlighter.","isBatchedStockType":false,"PurchasePrice":0.0,"RetailPrice":null,"TaxRate":0.0,"PostalServiceId":"00000000-0000-0000-0000-000000000000","PostalServiceName":null,"CategoryId":"b4e35a83-0e63-4e85-995f-5a99ce998fcf","CategoryName":"Linnworks Test Category","PackageGroupId":"00000000-0000-0000-0000-000000000000","PackageGroupName":null,"Height":0.0,"Width":0.0,"Depth":0.0,"Weight":0.1,"CreationDate":"2018-01-02T09:21:36.163","InventoryTrackingType":0,"BatchNumberScanRequired":false,"SerialNumberScanRequired":false,"StockItemId":"c3566ffc-9a7a-4de1-97eb-77d250a7bf25"},{"Suppliers":[],"StockLevels":[{"Location":{"StockLocationId":"00000000-0000-0000-0000-000000000000","LocationName":"Default","IsFulfillmentCenter":false,"LocationTag":null,"BinRack":""},"StockLevel":19,"StockValue":0.0,"MinimumLevel":-1,"InOrderBook":0,"Due":0,"InOrders":0,"Available":19,"UnitCost":0.0,"SKU":null,"AutoAdjust":false,"LastUpdateDate":"2018-01-02T09:21:40","LastUpdateOperation":"Composite Item Changed","rowid":"00000000-0000-0000-0000-000000000000","PendingUpdate":false,"StockItemId":"5dfeed36-a31d-41a6-aa4a-363b1423f63e"}],"ItemChannelDescriptions":[],"ItemExtendedProperties":[],"ItemChannelTitles":[],"ItemChannelPrices":[],"Images":[],"ItemNumber":"TEST_SS7050-X5","ItemTitle":"TEST_Swan Stabilo Boss Highlighter Yellow","BarcodeNumber":"40063811187400","MetaData":"Stabilo Boss Highlighter Yellow. The original highlighter since 1972. Available in a choice of attractive fluorescent colors with a long cap off time. The distinctive design stops the pen from rolling away and makes it usable anywhere. Brand quality from Schwann Stabilo, the inventor of the highlighter.","isBatchedStockType":false,"PurchasePrice":0.0,"RetailPrice":null,"TaxRate":0.0,"PostalServiceId":"00000000-0000-0000-0000-000000000000","PostalServiceName":null,"CategoryId":"b4e35a83-0e63-4e85-995f-5a99ce998fcf","CategoryName":"Linnworks Test Category","PackageGroupId":"00000000-0000-0000-0000-000000000000","PackageGroupName":null,"Height":0.0,"Width":0.0,"Depth":0.0,"Weight":0.5,"CreationDate":"2018-01-02T09:21:36.167","InventoryTrackingType":0,"BatchNumberScanRequired":false,"SerialNumberScanRequired":false,"StockItemId":"5dfeed36-a31d-41a6-aa4a-363b1423f63e"},{"Suppliers":[],"StockLevels":[{"Location":{"StockLocationId":"00000000-0000-0000-0000-000000000000","LocationName":"Default","IsFulfillmentCenter":false,"LocationTag":null,"BinRack":"C01-0001"},"StockLevel":100,"StockValue":0.0,"MinimumLevel":20,"InOrderBook":0,"Due":0,"InOrders":0,"Available":100,"UnitCost":0.0,"SKU":null,"AutoAdjust":false,"LastUpdateDate":"2018-01-02T09:21:36","LastUpdateOperation":"OPENING STOCK","rowid":"00000000-0000-0000-0000-000000000000","PendingUpdate":false,"StockItemId":"d6053ec6-a921-4bb2-b02c-946ad988316e"}],"ItemChannelDescriptions":[],"ItemExtendedProperties":[],"ItemChannelTitles":[],"ItemChannelPrices":[],"Images":[],"ItemNumber":"TEST_SS7051","ItemTitle":"TEST_Swan Stabilo Boss Highlighter Blue","BarcodeNumber":"4006381118741","MetaData":"Stabilo Boss Highlighter Blue. The original highlighter since 1972. Available in a choice of attractive fluorescent colors with a long cap off time. The distinctive design stops the pen from rolling away and makes it usable anywhere. Brand quality from Schwann Stabilo, the inventor of the highlighter.","isBatchedStockType":false,"PurchasePrice":0.0,"RetailPrice":null,"TaxRate":0.0,"PostalServiceId":"00000000-0000-0000-0000-000000000000","PostalServiceName":null,"CategoryId":"b4e35a83-0e63-4e85-995f-5a99ce998fcf","CategoryName":"Linnworks Test Category","PackageGroupId":"00000000-0000-0000-0000-000000000000","PackageGroupName":null,"Height":0.0,"Width":0.0,"Depth":0.0,"Weight":0.1,"CreationDate":"2018-01-02T09:21:36.547","InventoryTrackingType":0,"BatchNumberScanRequired":false,"SerialNumberScanRequired":false,"StockItemId":"d6053ec6-a921-4bb2-b02c-946ad988316e"},{"Suppliers":[],"StockLevels":[{"Location":{"StockLocationId":"00000000-0000-0000-0000-000000000000","LocationName":"Default","IsFulfillmentCenter":false,"LocationTag":null,"BinRack":""},"StockLevel":20,"StockValue":0.0,"MinimumLevel":-1,"InOrderBook":0,"Due":0,"InOrders":0,"Available":20,"UnitCost":0.0,"SKU":null,"AutoAdjust":false,"LastUpdateDate":"2018-01-02T09:21:39","LastUpdateOperation":"Composite Item Changed","rowid":"00000000-0000-0000-0000-000000000000","PendingUpdate":false,"StockItemId":"201b64d5-ca88-4a53-95e9-50a552148104"}],"ItemChannelDescriptions":[],"ItemExtendedProperties":[],"ItemChannelTitles":[],"ItemChannelPrices":[],"Images":[],"ItemNumber":"TEST_SS7051-X5","ItemTitle":"TEST_Swan Stabilo Boss Highlighter Blue","BarcodeNumber":"40063811187411","MetaData":"Stabilo Boss Highlighter Blue. The original highlighter since 1972. Available in a choice of attractive fluorescent colors with a long cap off time. The distinctive design stops the pen from rolling away and makes it usable anywhere. Brand quality from Schwann Stabilo, the inventor of the highlighter.","isBatchedStockType":false,"PurchasePrice":0.0,"RetailPrice":null,"TaxRate":0.0,"PostalServiceId":"00000000-0000-0000-0000-000000000000","PostalServiceName":null,"CategoryId":"b4e35a83-0e63-4e85-995f-5a99ce998fcf","CategoryName":"Linnworks Test Category","PackageGroupId":"00000000-0000-0000-0000-000000000000","PackageGroupName":null,"Height":0.0,"Width":0.0,"Depth":0.0,"Weight":0.5,"CreationDate":"2018-01-02T09:21:36.55","InventoryTrackingType":0,"BatchNumberScanRequired":false,"SerialNumberScanRequired":false,"StockItemId":"201b64d5-ca88-4a53-95e9-50a552148104"},{"Suppliers":[],"StockLevels":[{"Location":{"StockLocationId":"00000000-0000-0000-0000-000000000000","LocationName":"Default","IsFulfillmentCenter":false,"LocationTag":null,"BinRack":"C01-0002"},"StockLevel":100,"StockValue":0.0,"MinimumLevel":20,"InOrderBook":0,"Due":0,"InOrders":0,"Available":100,"UnitCost":0.0,"SKU":null,"AutoAdjust":false,"LastUpdateDate":"2018-01-02T09:21:36","LastUpdateOperation":"OPENING STOCK","rowid":"00000000-0000-0000-0000-000000000000","PendingUpdate":false,"StockItemId":"92b07b7d-73f9-421e-8e31-37dcbdc5f651"}],"ItemChannelDescriptions":[],"ItemExtendedProperties":[],"ItemChannelTitles":[],"ItemChannelPrices":[],"Images":[],"ItemNumber":"TEST_SS7052","ItemTitle":"TEST_Swan Stabilo Boss Highlighter Green","BarcodeNumber":"4006381118742","MetaData":"Stabilo Boss Highlighter Green. The original highlighter since 1972. Available in a choice of attractive fluorescent colors with a long cap off time. The distinctive design stops the pen from rolling away and makes it usable anywhere. Brand quality from Schwann Stabilo, the inventor of the highlighter.","isBatchedStockType":false,"PurchasePrice":0.0,"RetailPrice":null,"TaxRate":0.0,"PostalServiceId":"00000000-0000-0000-0000-000000000000","PostalServiceName":null,"CategoryId":"b4e35a83-0e63-4e85-995f-5a99ce998fcf","CategoryName":"Linnworks Test Category","PackageGroupId":"00000000-0000-0000-0000-000000000000","PackageGroupName":null,"Height":0.0,"Width":0.0,"Depth":0.0,"Weight":0.1,"CreationDate":"2018-01-02T09:21:37.133","InventoryTrackingType":0,"BatchNumberScanRequired":false,"SerialNumberScanRequired":false,"StockItemId":"92b07b7d-73f9-421e-8e31-37dcbdc5f651"}

The aim of the exercise is to retrieve the StockLevel and Available values for each of the items returned but this is the actual outcome:

USER_DEBUG [42]|DEBUG|MAP: {100=98}

Can anyone assist in why this is happening?

  • Your JSON (as posted) is invalid. It's hard to see since you just included your JSON as a massive, single line, but putting it through a JSON validator makes the issue clear. Your JSON includes many fromJSON top-level objects. It seems you've completely ignored what I said yesterday. Your JSON should be a list, and you need to deserialize to a list. – Derek F Jan 18 '18 at 14:27
  • In fact, now that I think on it, how is this different from your question from yesterday? – Derek F Jan 18 '18 at 14:27
1

If I put that json in VSCode and format it, I see that it's not happy with the format, as it's expecting the end of a file. You can fix that by wrapping it in '[' and ']' to make it a json array. You'll also then need a container type and to add a key for it to the json too. It's a bit of a faff but I've not found a better way so far. You then need to adjust your code to support that, so it turns out looking something a bit like this probably:

public class fromJSON {
    // The top level is now just a list of items
    public JsonItem[] items;

    // Move all the attributes down one level
    public class JsonItem {
        public String StockItemId;
        public cls_Suppliers[] Suppliers;
        public StockLevels[] stockLevels;
        public cls_ItemChannelDescriptions[] ItemChannelDescriptions;

        ... lots of other variable definitions ...
    }

    ... all the other inner class definitions ...

    public static fromJSON parse(String json) {
        // Wrap the JSON into it's own object so that deserialize can handle it
        String jsonObject = '{"items":' + json + '}';
        return (fromJSON) System.JSON.deserialize(jsonObject, fromJSON.class);
    }
}

As the structure of the JSON is like this (you have multiple StockLevels):

{
    "Suppliers": [<always empty in your json>],
    "StockLevels": [<only ever has one element in your json but could have more I guess?>],
    ... <selection of other key/value pairs ...
},
    {
    "Suppliers": [<always empty in your json>],
    "StockLevels": [<only ever has one element in your json>],
    ... <selection of other key/value pairs ...
}

And because we've moved your fromJson object a bit, your processing class will need adjusting too, to something like this:

try {

    ... get the json response ...

    fromJSON r = fromJSON.parse(res.getBody());

    Map<Integer, Integer> myMap = new Map<Integer, Integer>();

    for(fromJson.JsonItem item : fromJSON.items) {
        for (fromJson.StockLevels stocklevels : item.stocklevels) {
            myMap.put(stocklevels.StockLevel, stocklevels.Available);
        }
    }
    System.debug('MAP: '+ myMap);

    ... helpful debug ...

} catch (System.CalloutException e) {
    //Exception handling goes here....
}

Hope this helps. Would like there to be an easier / explicitly supported way of doing this but I've not found one yet - would love to be corrected though!

Edit As @Derek F said in your question yesterday, if you have control over the JSON coming in, then the absolute best place to fix the formatting / structure would be there, rather than in your Salesforce code! :)

  • Thanks @TomB, the response is greatly appreciated! Unfortunately we dont have control over the JSON that is coming into Salesforce. I'll give your method a try! – Salesf0rce Jan 18 '18 at 14:59
  • Glad I could help @Salesf0rce :) – TomB Jan 19 '18 at 9:18

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.