Monday, September 29, 2025

Read json using Newtonsoft.Json x++ d365fo

 Request message:

{

  "docType": {

    "alias": "RANDOM_DOC"

  },

  "streamName": "abc123",

  "fromParty": {

    "idTag": "SUPPLIER_XYZ"

  },

  "toParty": {

    "idTag": "BUYER_ABC"

  },

  "payload": {

    "records": [

      {

        "headerBlock": [

          {

            "headerCode": "999",

            "headerNumber": "001"

          }

        ],

        "beginBlock": [

          {

            "purpose": "06",

            "shipmentId": "SHIP123",

            "dateVal": "20230930",

            "timeVal": "1210",

            "structCode": "0001"

          }

        ],

        "loopBlock": [

          {

            "levelBlock": [

              {

                "levelId": "1",

                "parentLevel": "0",

                "levelCode": "S"

              }

            ],

            "carrierBlock": [

              {

                "packCode": "CAS",

                "qty": "2",

                "weightType": "G",

                "weightVal": "4.5",

                "uom": "LB"

              }

            ]

          },

          {

            "levelBlock": [

              {

                "levelId": "2",

                "parentLevel": "1",

                "levelCode": "O"

              }

            ],

            "orderRef": [

              {

                "orderNum": "PO987654321"

              }

            ]

          },

          {

            "levelBlock": [

              {

                "levelId": "3",

                "parentLevel": "2",

                "levelCode": "I"

              }

            ],

            "itemBlock": [

              {

                "itemId": "1234567890",

                "sku": "SKU-1111"

              }

            ],

            "descBlock": [

              {

                "text": "Random Sunglasses (X1001)"

              }

            ],

            "shipBlock": [

              {

                "lineId": "10",

                "qtyShipped": "25",

                "unitCode": "EA"

              }

            ]

          }

        ]

      }

    ]

  }

}

-------------------------------------------------------------------------------------------------------------------
class RandomJsonRunnable
{
    public static void main(Args args)
    {
        Dialog dialog = new Dialog("Random JSON Parser");
        DialogField field = dialog.addField(extendedTypeStr(Notes), "Paste Random JSON:");
        dialog.run();
        if (dialog.closedOk())
        {
            RandomJsonRunnable::readJson(field.value());
        }
    }

    public static str readJson(Notes _notes)
    {
        str response;
        try
        {
            // Root JSON object
            Newtonsoft.Json.Linq.JObject root = Newtonsoft.Json.JsonConvert::DeserializeObject(_notes);

            // Navigate down: payload → records
            Newtonsoft.Json.Linq.JObject payload = root.GetValue('payload');
            Newtonsoft.Json.Linq.JArray records = payload.GetValue('records');

            for (int r = 0; r < records.Count; r++)
            {
                Newtonsoft.Json.Linq.JObject record = records.get_Item(r);

                // loopBlock array
                Newtonsoft.Json.Linq.JArray loopBlock = record.GetValue('loopBlock');

                str orderNum = "";
                for (int l = 0; l < loopBlock.Count; l++)
                {
                    Newtonsoft.Json.Linq.JObject loop = loopBlock.get_Item(l);

                    Newtonsoft.Json.Linq.JArray levelBlock = loop.GetValue('levelBlock');
                    str levelCode = "";
                    if (levelBlock && levelBlock.Count > 0)
                    {
                        Newtonsoft.Json.Linq.JObject levelObj = levelBlock.get_Item(0);
                        levelCode = levelObj.GetValue('levelCode') ? levelObj.GetValue('levelCode').ToString() : "";

                        
                            Newtonsoft.Json.Linq.JArray orderRef = loop.GetValue('orderRef');
                            if (orderRef && orderRef.Count > 0)
                            {
                                Newtonsoft.Json.Linq.JObject orderRefObj = orderRef.get_Item(0);
                                orderNum = orderRefObj.GetValue('orderNum') ? orderRefObj.GetValue('orderNum').ToString() : "";
                            }

                        
                            Newtonsoft.Json.Linq.JObject itemBlockObj = loop.GetValue('itemBlock').get_Item(0);
                            Newtonsoft.Json.Linq.JObject descBlockObj = loop.GetValue('descBlock').get_Item(0);
                            Newtonsoft.Json.Linq.JObject shipBlockObj = loop.GetValue('shipBlock').get_Item(0);

                            str itemId = (itemBlockObj && itemBlockObj.GetValue('itemId')) ? itemBlockObj.GetValue('itemId').ToString() : "";
                            str description = (descBlockObj && descBlockObj.GetValue('text')) ? descBlockObj.GetValue('text').ToString() : "";
                            str lineId = (shipBlockObj && shipBlockObj.GetValue('lineId')) ? shipBlockObj.GetValue('lineId').ToString() : "";
                            str qtyShipped = (shipBlockObj && shipBlockObj.GetValue('qtyShipped')) ? shipBlockObj.GetValue('qtyShipped').ToString() : "";
                            str unit = (shipBlockObj && shipBlockObj.GetValue('unitCode')) ? shipBlockObj.GetValue('unitCode').ToString() : "";

                            response = strFmt("Order:%1, Item:%2, Desc:%3, Line:%4, Qty:%5, Unit:%6",
                                orderNum, itemId, description, lineId, qtyShipped, unit);
                            
                    }
                }
            }
        }
        catch (Exception::CLRError)
        {
            error(AifUtil::getClrErrorMessage());
        }
        return response;
    }
}

Call API using x++ D365FO

 System.Net.HttpWebRequest    request; System.Net.HttpWebResponse   response; System.IO.Stream             dataStream; System.IO.StreamReade...