none
Parse complex JSON format

    Question

  • Hi there. Hope someone can guide me a little here. I'm trying parse the following JSON response and I'm getting an error. I've looked through most site for an answer but nothing has worked. Here is the JSON message I'm trying to parse:

    {
        "messageId": 293,
        "data": {
            “quotes”: [
                    {"material": [
      {
       “materialOrder”: “1”,
       “productSku”: “248S”,
       “productDescription”: “2X4X8 SPRUCE KD”,
       “quantity”: “100.0”,
       “unitOfMeasure”: “EA”,
       “manufacturer”: “RIVTOO”,
       “model”: “12345680574”,
       “materialCost”: “1.600”,
    “materialSales”: “2.13”,
    “lineTypeId”: “1”

                }
                            {
       “materialOrder”: “1”,
       “productSku”: “”,
       “productDescription”: “COMMENT”,
       “quantity”: “”,
       “unitOfMeasure”: “”,
       “manufacturer”: “”,
       “model”: “”,
       “materialCost”: “”,
    “materialSales”: “”,
    “lineTypeId”: “2”
                }

            ],
            “labour”: [
      {
       “labourOrder”: “1”,
       “labourDescription”: “LABOUR CHARGE”,
       “productSku”: “LAB”,
       “quantity”: “1”,
       “unitOfMeasure”: “”,
       “labourCost”: “150.000”,
       “labourSales”: “200.00”,
       “lineTypeId”: “1”
      }
            ],
            “miscellaneous”: [
      {
       “miscOrder”: “1”,
       “miscCostDescription”: “PRODUCT”,
       “productSku”: “1234”,
       “quantity”: “2”,
       “unitOfMeasure”: “EA”,
       “miscCost”: “1.600”,
       “miscSales”: “5.00”,
                        “lineTypeId”: “1”
      }
            ]
        }
    ]
                    }
    }

    This is the code I'm attempting to use but get an error with the JARRAY statement. Any help is appreciated.

    Thanks

    Imports Newtonsoft.Json.Linq
    
    Dim clientSocket As New System.Net.Sockets.TcpClient()
    Dim data As [Byte]()
    Dim bytes As Int32 = 0
    Dim returndata As String = Nothing
    Dim JSONResponse As JObject
    Dim stream As Net.Sockets.NetworkStream = clientSocket.GetStream()
    Dim CustStringDelimit As String = Nothing
    
    clientSocket = New System.Net.Sockets.TcpClient()
    clientSocket.Connect(JSONServerName, 8888)
    stream = clientSocket.GetStream()
    CustStringDelimit = "SEND JSON REQUEST MESSAGE"
    data = System.Text.Encoding.ASCII.GetBytes(CustStringDelimit)
    stream.Write(data, 0, data.Length)
    data = New [Byte](10240) {}
    bytes = stream.Read(data, 0, data.Length)
    returndata = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
    JSONResponse = JObject.Parse(returndata)
    
    Dim results As List(Of JToken) = JSONResponse.Item("data").Children().Children().ToList
           For Each Quotes As JArray In results
                 For Each AllCosts As JProperty In Quotes
                       AllCosts.CreateReader()
                       Select Case AllCosts.Name
                                Case "material"
                                        For Each CostField As JObject In AllCosts.Values
                                        NEXT
                      END Select
                 NEXT
          NEXT



    Dweezil

    Thursday, April 27, 2017 6:50 PM

Answers

  • If the formatting issue was just something in the post and not in the original code then the problem may be that JArray should actually be a JObject instance. I'm assuming the children of "data" would be the "quotes" and the children of "quotes" would be the "materials" array.  That array contains JSON objects.  So you either meant to use a JObject here or you've gone one level too deep in the child collections. 

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, May 03, 2017 8:13 PM
    Moderator

All replies

  • Dweezil,

    I may be incorrect (it wouldn't be the first time), but that JSON string doesn't seem to be valid JSON.

    Is that maybe an excerpt from something larger?


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, April 27, 2017 7:45 PM
  • Hi There

    This is the JSON Message I get and it is complete.


    Dweezil

    Wednesday, May 03, 2017 2:20 PM
  • Hi There

    This is the JSON Message I get and it is complete.


    Dweezil

    Everything that I've used to try to parse it is reporting that it's invalid.

    Unlike XML, I can't look at it and tell.

    Hopefully someone else can help here.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Wednesday, May 03, 2017 3:00 PM
  • You appear to have some kind of encoding problem.  There are invalid characters in the text you posted.  It looks OK at a glance, but look closer... particularly the double-quote marks beginning on the 4th line.

    If I do some text replacements and conversions, I can get that JSON to parse correctly.  Here's the clean text:

    {
      "messageId": 293,
      "data": {
        "quotes": [
          {
            "material": [
              {
                "materialOrder": "1",
                "productSku": "248S",
                "productDescription": "2X4X8 SPRUCE KD",
                "quantity": "100.0",
                "unitOfMeasure": "EA",
                "manufacturer": "RIVTOO",
                "model": "12345680574",
                "materialCost": "1.600",
                "materialSales": "2.13",
                "lineTypeId": "1"
              },
              {
                "materialOrder": "1",
                "productSku": "",
                "productDescription": "COMMENT",
                "quantity": "",
                "unitOfMeasure": "",
                "manufacturer": "",
                "model": "",
                "materialCost": "",
                "materialSales": "",
                "lineTypeId": "2"
              }
            ],
            "labour": [
              {
                "labourOrder": "1",
                "labourDescription": "LABOUR CHARGE",
                "productSku": "LAB",
                "quantity": "1",
                "unitOfMeasure": "",
                "labourCost": "150.000",
                "labourSales": "200.00",
                "lineTypeId": "1"
              }
            ],
            "miscellaneous": [
              {
                "miscOrder": "1",
                "miscCostDescription": "PRODUCT",
                "productSku": "1234",
                "quantity": "2",
                "unitOfMeasure": "EA",
                "miscCost": "1.600",
                "miscSales": "5.00",
                "lineTypeId": "1"
              }
            ]
          }
        ]
      }
    }
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, May 03, 2017 5:48 PM
    Moderator
  • If the formatting issue was just something in the post and not in the original code then the problem may be that JArray should actually be a JObject instance. I'm assuming the children of "data" would be the "quotes" and the children of "quotes" would be the "materials" array.  That array contains JSON objects.  So you either meant to use a JObject here or you've gone one level too deep in the child collections. 

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, May 03, 2017 8:13 PM
    Moderator