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?
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