jsonzohozoho-deluge

How to access a json array using Zoho Deluge's POST method?


I am having a bit of trouble accessing Zoho inventory's api which has a data array, this is how it looks

{
    "date": "2015-05-28",
    "reason": "Damaged goods",
    "description": "Just a sample description.",
    "reference_number": "REF-IA-00001",
    "adjustment_type": "quantity",
    "line_items": [
        {
            "item_id": 4815000000044100,
            "name": "Laptop-white/15inch/dell",
            "description": "Just a sample description.",
            "quantity_adjusted": 10,
            "item_total": 244,
            "unit": "qty",
            "is_combo_product": false,
            "adjustment_account_id": 4815000000000388,
            "adjustment_account_name": "Cost of Goods Sold",
            "warehouse_id": 4815000000000390,
            "warehouse_name": "MyWarehouse"
        }
    ]
}

This my current code

headersMap = Map();
headersMap.put("Authorization","Zoho-oauthtoken xxxxxxxxxxxxxxxxx");
parametersMap = Map();
response = invokeurl
[
    url :"https://inventory.zoho.com/api/v1/inventoryadjustments?organization_id=xxxx"
    type :POST
    headers:headersMap
];
dateVal = input.Date_field;
reasonVal = input.Reason;
descriptionVal = input.Description1;
referenceNumberVal = input.Reference_Number;
adjustmentTypeVal = input.Adjustment_Type;
itemIDVal = input.Item_ID;
nameVal = input.Name1;
quantityAdjustedVal = input.Quantity_Adjusted;
itemTotalVal = input.Item_Total;
unitVal = input.Unit;
isComboProductVal = input.Is_Combo_Product;
adjustmentAccountIdVal = input.Adjustment_Account_ID;
adjustmentAccountNameVal = input.Adjustment_Account_Name;
warehouseIdVal = input.Warehouse_ID;
warehouseNameVal = input.Warehouse_Name;

parametersMap.put("date",dateVal);
parametersMap.put("reason",reasonVal);
parametersMap.put("description",descriptionVal);
parametersMap.put("reference_number",referenceNumberVal);
parametersMap.put("adjustment_type",adjustmentTypeVal);

I have already created variables for all the values, but I am bit confused on how I am supposed to write the keys for "line_items" array, any help on this would be greatly appreciated.


Solution

  • Revision 2:

    1. It looks like "line_items" is a List of Maps. One Map for each item. The "line_item" specific code in the example has the "Line_Items:" headers in the comments.

    2. It looks like the parameterMap should be setup before the call to invokeurl.

    Then invokeurl should include the parameterMap.

    Example:

    headersMap = Map();
    headersMap.put("Authorization","Zoho-oauthtoken xxxxxxxxxxxxxxxxx");
    
    // Setup parametersMap
    //----------------------------------------------------
    parametersMap = Map();
    
    dateVal = input.Date_field;
    reasonVal = input.Reason;
    descriptionVal = input.Description1;
    referenceNumberVal = input.Reference_Number;
    adjustmentTypeVal = input.Adjustment_Type;
    itemIDVal = input.Item_ID;
    nameVal = input.Name1;
    quantityAdjustedVal = input.Quantity_Adjusted;
    itemTotalVal = input.Item_Total;
    unitVal = input.Unit;
    isComboProductVal = input.Is_Combo_Product;
    adjustmentAccountIdVal = input.Adjustment_Account_ID;
    adjustmentAccountNameVal = input.Adjustment_Account_Name;
    warehouseIdVal = input.Warehouse_ID;
    warehouseNameVal = input.Warehouse_Name;
    
    //----------------------------------------------------
    // Line_Items:
    // Configure an item's data as a Deluge Map
    //----------------------------------------------------
    OneItem = Map();
    OneItem.put("item_id", 4815000000044100);
    OneItem.put("name", "Laptop-white/15inch/dell");
    OneItem.put("description", "Just a sample description.");
    OneItem.put("quantity_adjusted", 10);
    OneItem.put("item_total", 244);
    OneItem.put("unit", "qty");
    OneItem.put("is_combo_product", false);
    OneItem.put("adjustment_account_id", 4815000000000388);
    OneItem.put("adjustment_account_name", "Cost of Goods Sold");
    OneItem.put("warehouse_id", 4815000000000390);
    OneItem.put("warehouse_name", "MyWarehouse");
    
    //----------------------------------------------------
    // Line_Items:
    // Add the items(s) to a line_items Deluge List
    //----------------------------------------------------
    MyItems = List();
    MyItems.add(OneItem);
    
    //----------------------------------------------------
    // Line_Items:
    // Add the line_items Deluge List to the parametersMap
    //----------------------------------------------------
    parametersMap.put.("line_items", MyItems);
    
    parametersMap.put("date",dateVal);
    parametersMap.put("reason",reasonVal);
    parametersMap.put("description",descriptionVal);
    parametersMap.put("reference_number",referenceNumberVal);
    parametersMap.put("adjustment_type",adjustmentTypeVal);
    
    // Then call invokeurl
    // - Include: parameters:parametersMap
    //----------------------------------------------------
    response = invokeurl
    [
        url :"https://inventory.zoho.com/api/v1/inventoryadjustments?organization_id=xxxx"
        type :POST
        headers:headersMap
        parameters:parametersMap
    ];
    
    // display the response
    info response;