Deserializing JSON with embedded classes and arrays.

Need to parse a JSON object that contains embedded classes and arrays for Dynamics GP.

 

Sample JSON:

{
  "totalItemCount": 7,
  "pageIndex": 0,
  "pageSize": 20,
  "pagedData": [
    {
      "orderId": "6f98e70f-85ba-45ad-8474-e9b5cd409a39",
      "invoiceNumber": "2018050309232244AFM27JTLLAQ",
      "orderDate": "2018-05-03T04:23:22.0000000",
      "subTotal": "0.10",
      "taxTotal": "0",
      "shippingTotal": "0",
      "processingFeeTotal": "0.00",
      "orderTotal": "0",
      "orderDetail": [
        {
          "orderDetailId": "1bd708ee-1abe-4b19-9541-ced7988ff177",
          "orderId": "6f98e70f-85ba-45ad-8474-e9b5cd409a39",
          "productId": "dd5d6ce1-3663-42aa-a650-36ba40699000",
          "quantity": 1,
          "productPrice": "0.10",
          "sku": "SKU_001002",
          "productName": "(test only) beautiful flowers"
        }
      ],
      "accountingOrderTypeId": "c0aaefe9-4144-48ea-8a66-a539352d381e",
      "accountingOrderTypeName": "Mobile",
      "posCatalogName": "",
      "subTotalBeforeDiscounts": "0.10",
      "shippingTotalBeforeDiscounts": "0"
    },
    {
      "orderId": "3fa33505-fefd-4c1e-83bc-70d950c8272c",
      "invoiceNumber": "20180504074446767TYVXRC0CVSO",
      "orderDate": "2018-05-04T02:44:46.0000000",
      "subTotal": "0.10",
      "taxTotal": "0",
      "shippingTotal": "0",
      "processingFeeTotal": "0.00",
      "orderTotal": "0",
      "orderDetail": [
        {
          "orderDetailId": "dc81986c-5d7c-4e31-96c6-b889ba58cca4",
          "orderId": "3fa33505-fefd-4c1e-83bc-70d950c8272c",
          "productId": "dd5d6ce1-3663-42aa-a650-36ba40699000",
          "quantity": 1,
          "productPrice": "0.10",
          "sku": "SKU_001002",
          "productName": "(test only) beautiful flowers"
        }
      ],
      "accountingOrderTypeId": "c0aaefe9-4144-48ea-8a66-a539352d381e",
      "accountingOrderTypeName": "Mobile",
      "posCatalogName": "",
      "subTotalBeforeDiscounts": "0.10",
      "shippingTotalBeforeDiscounts": "0"
    },
    {
      "orderId": "4c27c368-4af8-4107-9c9a-fb6f3e0964b7",
      "invoiceNumber": "20180504080648354UM374RJFWJX",
      "orderDate": "2018-05-04T03:06:48.0000000",
      "subTotal": "0.10",
      "taxTotal": "0",
      "shippingTotal": "0",
      "processingFeeTotal": "0.00",
      "orderTotal": "0",
      "orderDetail": [
        {
          "orderDetailId": "f06cac63-526b-41e3-b2a5-9a50d62b8f3c",
          "orderId": "4c27c368-4af8-4107-9c9a-fb6f3e0964b7",
          "productId": "dd5d6ce1-3663-42aa-a650-36ba40699000",
          "quantity": 1,
          "productPrice": "0.10",
          "sku": "SKU_001002",
          "productName": "(test only) beautiful flowers"
        }
      ],
      "accountingOrderTypeId": "c0aaefe9-4144-48ea-8a66-a539352d381e",
      "accountingOrderTypeName": "Mobile",
      "posCatalogName": "",
      "subTotalBeforeDiscounts": "0.10",
      "shippingTotalBeforeDiscounts": "0"
    },
    {
      "orderId": "ea430036-08cf-4203-880b-264123ea323e",
      "invoiceNumber": "20180504113951344IK6VHPJX9IP",
      "orderDate": "2018-05-04T06:39:51.0000000",
      "subTotal": "0.10",
      "taxTotal": "0",
      "shippingTotal": "0",
      "processingFeeTotal": "0.00",
      "orderTotal": "0",
      "orderDetail": [
        {
          "orderDetailId": "7a0c556d-5262-499f-aa19-4210714348f8",
          "orderId": "ea430036-08cf-4203-880b-264123ea323e",
          "productId": "dd5d6ce1-3663-42aa-a650-36ba40699000",
          "quantity": 1,
          "productPrice": "0.10",
          "sku": "SKU_001002",
          "productName": "(test only) beautiful flowers"
        }
      ],
      "accountingOrderTypeId": "c0aaefe9-4144-48ea-8a66-a539352d381e",
      "accountingOrderTypeName": "Mobile",
      "posCatalogName": "",
      "subTotalBeforeDiscounts": "0.10",
      "shippingTotalBeforeDiscounts": "0"
    },
    {
      "orderId": "77c4822d-ff6a-4c49-bdcb-0f75aa29d258",
      "invoiceNumber": "20180504114302195NQ13D1BPDR7",
      "orderDate": "2018-05-04T06:43:02.0000000",
      "subTotal": "0.10",
      "taxTotal": "0",
      "shippingTotal": "0",
      "processingFeeTotal": "0.00",
      "orderTotal": "0",
      "orderDetail": [
        {
          "orderDetailId": "3854f0da-0e89-48b9-a83b-5f8a40e2a50d",
          "orderId": "77c4822d-ff6a-4c49-bdcb-0f75aa29d258",
          "productId": "dd5d6ce1-3663-42aa-a650-36ba40699000",
          "quantity": 1,
          "productPrice": "0.10",
          "sku": "SKU_001002",
          "productName": "(test only) beautiful flowers"
        }
      ],
      "accountingOrderTypeId": "c0aaefe9-4144-48ea-8a66-a539352d381e",
      "accountingOrderTypeName": "Mobile",
      "posCatalogName": "",
      "subTotalBeforeDiscounts": "0.10",
      "shippingTotalBeforeDiscounts": "0"
    },
    {
      "orderId": "f08dd1a1-69d1-4fe6-a19d-096e0cc462df",
      "invoiceNumber": "20180504125640107DUGQ60L72GC",
      "orderDate": "2018-05-04T07:56:40.0000000",
      "subTotal": "6.75",
      "taxTotal": "0",
      "shippingTotal": "0",
      "processingFeeTotal": "0.34",
      "orderTotal": "0",
      "orderDetail": [
        {
          "orderDetailId": "bbec5ccc-e214-4e3e-a5df-a2c7fbdb5af2",
          "orderId": "f08dd1a1-69d1-4fe6-a19d-096e0cc462df",
          "productId": "87b40cc5-5f41-46de-8336-27ccde97c8f6",
          "quantity": 1,
          "productPrice": "6.75",
          "sku": "LTDQA-004-GMBNDL",
          "productName": "10th anniversary celebration"
        }
      ],
      "accountingOrderTypeId": "c0aaefe9-4144-48ea-8a66-a539352d381e",
      "accountingOrderTypeName": "Mobile",
      "posCatalogName": "",
      "subTotalBeforeDiscounts": "6.75",
      "shippingTotalBeforeDiscounts": "0"
    },
    {
      "orderId": "09e66c4e-61c5-48dc-964c-26f5a2f4b7bd",
      "invoiceNumber": "20180504083011249KAXGIETOQ0K",
      "orderDate": "2018-05-04T08:30:02.0000000",
      "subTotal": "59.95",
      "taxTotal": "0",
      "shippingTotal": "0",
      "processingFeeTotal": "0",
      "orderTotal": "0",
      "orderDetail": [
        {
          "orderDetailId": "125b33c4-2084-4026-b54d-fcb9ee6d6183",
          "orderId": "09e66c4e-61c5-48dc-964c-26f5a2f4b7bd",
          "productId": "c4546911-ac06-45c1-ad4c-8780c24630a3",
          "quantity": 1,
          "productPrice": "59.95",
          "sku": "S16-1963-NMB",
          "productName": "New Member Basic"
        }
      ],
      "accountingOrderTypeId": "ae8d6853-fb1f-42d9-9b96-f8bb744336fe",
      "accountingOrderTypeName": "Automatic Subscription Renewal",
      "posCatalogName": "",
      "subTotalBeforeDiscounts": "59.95",
      "shippingTotalBeforeDiscounts": "0"
    }
  ]
}

 

 

 

Here is a sample Console app that reads the JSON string from a file and parses it.  

 

Note that you can create the JSON class(es) automatically:

  • Create a new class in Visual Studio
  • Import System.Runtime.Serialization and System.Runtime.Serialization.Json
  • Add a reference to System.Runtime.Serialization (if not already included)
  • Copy the JSON to the clipboard
  • Place cursor in new class
  • Select Edit -> Paste Special -> Paste JSON As Classes
  • Add the <DataContract> decorator to each JSON class
  • Add the <DataMember> decorator to each property in each of the JSON classes
  • Note that you must move the () from the field name to the type for collections

 

Sample code:

Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Json
Imports System.Text
 
Module Module1
 
    Sub Main()
 
        Dim strJSON As String
        Dim o As New StreamReader("Data.json")
        strJSON = o.ReadToEnd
 
        ' Deserializing Json object from string
        Dim oSerializer As New DataContractJsonSerializer(GetType(Rootobject))
 
        ' Read JSON data into memory stream and cast to top-level object
        Dim ms = New MemoryStream(Encoding.Unicode.GetBytes(strJSON))
        Dim oDeSerJSON As Rootobject = DirectCast(oSerializer.ReadObject(ms), Rootobject)
 
        Dim numItems As Integer = Convert.ToInt32(oDeSerJSON.totalItemCount)
        For Each pd As pagedData In oDeSerJSON.pagedData
            Dim s As String = pd.invoiceNumber
            For Each od As orderDetail In pd.orderDetail
                Dim orderId As String = od.orderId
                Dim prodPrice As Decimal = Convert.ToDecimal(od.productPrice)
            Next
        Next
    End Sub
 
    <DataContract>
    Public Class Rootobject
        <DataMember>
        Public Property totalItemCount As Integer
        <DataMember>
        Public Property pageIndex As Integer
        <DataMember>
        Public Property pageSize As Integer
 
        ' Must move () from field name to type
        <DataMember>
        Public Property pagedData As pagedData()
    End Class
 
    <DataContract>
    Public Class pagedData
        <DataMember>
        Public Property orderId As String
        <DataMember>
        Public Property invoiceNumber As String
        <DataMember>
        Public Property orderDate As String
        <DataMember>
        Public Property subTotal As String
        <DataMember>
        Public Property taxTotal As String
        <DataMember>
        Public Property shippingTotal As String
        <DataMember>
        Public Property processingFeeTotal As String
        <DataMember>
        Public Property orderTotal As String
 
        ' Must move () from field name to type
        <DataMember>
        Public Property orderDetail As orderDetail()
        <DataMember>
        Public Property accountingOrderTypeId As String
        <DataMember>
        Public Property accountingOrderTypeName As String
        <DataMember>
        Public Property posCatalogName As String
        <DataMember>
        Public Property subTotalBeforeDiscounts As String
        <DataMember>
        Public Property shippingTotalBeforeDiscounts As String
    End Class
 
    <DataContract>
    Public Class orderDetail
        <DataMember>
        Public Property orderDetailId As String
        <DataMember>
        Public Property orderId As String
        <DataMember>
        Public Property productId As String
        <DataMember>
        Public Property quantity As Integer
        <DataMember>
        Public Property productPrice As String
        <DataMember>
        Public Property sku As String
        <DataMember>
        Public Property productName As String
    End Class
 
End Module

 

 

 

 

 


RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences