0

I'm not sure how to fix the following error and am looking for some help in this code I found that I have attempted to adapt for use in application embedded VB.Net:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[System.Object]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'error', line 1, position 9.

Below is the code That is generating this error

Dim OutputText As String
Dim Worksteps = JsonConvert.DeserializeObject(Of List(Of Object))(responseFromServer)
Dim token As JToken
Dim SheetName As String
Dim SheetStatus As String
Dim Completed As Boolean
For Each value As Object In Worksteps
    token = JObject.Parse(value.ToString())
    SheetName = token.SelectToken("name")
    SheetStatus = token.SelectToken("status")
    Dim parts() As String = SheetName.Split(" "c)
    If parts(0) = "Sheet_1" Then
        If SheetStatus = "COMPLETED" Then
            Completed = True
            Exit For
        End If
    End If
Next value
Select Case Completed
Case True
    OutputText = "Sheet_1 Status is COMPLETED"
Case False
    OutputText = "Sheet_1 Status is NOT COMPLETED"
End Select

Here is the JSON I'm attempting to parse. My assumption is that I somehow need to first parse for the Worksteps before trying to make a list of worksteps but I'm not sure how to do that.

{
  "error": null,
  "worksteps": [
    {
      "id": "_210504_125916572_029875",
      "name": "Sheet_1  4/4",
      "job": {
        "id": "060671-21",
        "name": "2021 Laramie High School"
      },
      "status": "COMPLETED",
      "amountPlanned": 544,
      "wastePlanned": 60,
      "amountProduced": 169,
      "wasteProduced": 69,
      "deviceId": "114",
      "types": [
        "ConventionalPrinting"
      ],
      "sequenceType": "SheetfedPrinting",
      "start": "2021-05-05T09:46:06-05:00",
      "end": "2021-05-05T09:48:38-05:00",
      "startPlanned": "2021-05-05T07:52:22-05:00",
      "endPlanned": null,
      "setuptimePlanned": 0,
      "prodtimePlanned": 0,
      "actualTimes": [
        {
          "timeTypeGroupName": "Production time",
          "timeTypeName": "Execution time",
          "duration": 33
        },
        {
          "timeTypeGroupName": "Production time",
          "timeTypeName": "Setup time",
          "duration": 79
        },
        {
          "timeTypeGroupName": "Auxiliary time",
          "timeTypeName": "Auxiliary time",
          "duration": 40
        },
        {
          "timeTypeGroupName": "",
          "timeTypeName": "",
          "duration": 0
        }
      ]
    },
    {
      "id": "_210506_072306983_020035",
      "name": "Sheet_2  4/4",
      "job": {
        "id": "060671-21",
        "name": "2021 Laramie High School"
      },
      "status": "WAITING",
      "amountPlanned": 0,
      "wastePlanned": 0,
      "amountProduced": 0,
      "wasteProduced": 0,
      "deviceId": "XL106_Pool",
      "types": [
        "ConventionalPrinting"
      ],
      "sequenceType": "SheetfedPrinting",
      "start": null,
      "end": null,
      "startPlanned": null,
      "endPlanned": null,
      "setuptimePlanned": 0,
      "prodtimePlanned": 0,
      "actualTimes": null
    }
  ]
}
dbc
  • 104,963
  • 20
  • 228
  • 340
TomD
  • 21
  • 3
  • Hi Tom, Welcome to Stackoverflow. There is already an answer to the issue you have. See this question + answer https://stackoverflow.com/q/21358493/1244816 – Jens Kloster May 18 '21 at 21:18
  • That Answer and question is in C# so it doesn't really answer my question in a way that I find actually useful for my VB problem. I get that I need to somehow select the workflow item to deserialize the array of workflow objects, but translating the suggested method from C# to VB is beyond my knowledge base. – TomD May 19 '21 at 12:42
  • To add to my above comment, I have zero C# experience so if someone would assist me with translating the C# syntax from that post suggested by Jens Kloster to VB so I can deserialize the "RootOject" and then make a list of the root object, that would be quite helpful. – TomD May 19 '21 at 13:16
  • Please keep in mind I'm not working in Visual Studio but rather an embedded version of VB in another application. VB is not my language of choice it is the language that is provided to create custom actions within the Kodak Prinergy Rules Based Automation environment. I normally write applications in another Object oriented version of Basic called Xojo, which I have no problem parsing JSON. I'm struggling to get VB to work for me to do this. – TomD May 19 '21 at 13:16
  • I would still like to know how to correctly parse the JSON sample I provided to extract the values I'm looking for, however, I found a workable solution to convert the JSON to XML and I had no problem getting the values I desired and can move on with my Prinergy RBA project. If you have an equivalent JSON solution please post that for future reference for anyone else struggling with JSON. – TomD May 19 '21 at 20:16
  • Here is a very good C# -> VB converter: https://www.tangiblesoftwaresolutions.com/ – SezMe Dec 06 '22 at 19:10

1 Answers1

1

Here is the solution I came up with to convert JSON sample above to XML and then extract the values for the sheet and status:

'Parse the JSON responseFromServer into an XMLDocument
Dim xdoc As New XmlDocument
' "job_worksteps" is the name being provided 
xdoc = JsonConvert.DeserializeXmlNode(responseFromServer,"job_worksteps")
' Look in the XML to determine the if a specified sheet has been completed
Dim nodes As XmlNodeList = xdoc.DocumentElement.SelectNodes("/job_worksteps/worksteps")
Dim Completed As Boolean
Dim SheetName, SheetStatus, OutputText As String
Dim TargetSheet As String = "Sheet_" + triggerEvent.String2 ' Integer as string included in the Prinergy RBA Trigger HTTP Post
' Loop through the XML nodes to find the SheetName node "name" and SheetStatus node "status"
For Each node As XmlNode In nodes
    SheetName = node.SelectSingleNode("name").InnerText
    SheetStatus = node.SelectSingleNode("status").InnerText
    Dim parts() As String = SheetName.Split("  ")
    ' Validate the TargetSheet name has a Status of "COMPLETED"
    If parts(0) = TargetSheet Then
    If SheetStatus = "COMPLETED" Then
        Completed = True
        Exit For
        End If
    End If
Next
' Format the output we want for this test
' (The final Boolean value will actually be used to trigger another function if it is True)
Select Case Completed
Case True
    OutputText = "Sheet_1 Status is COMPLETED"
Case False
    OutputText = "Sheet_1 Status is NOT COMPLETED"
End Select

Here is the resulting XML from some additional code to append it to the OutputText variable:

Sheet_1 Status is COMPLETED

<job_worksteps>
    <error />
    <worksteps>
        <id>_210505_073301677_027121</id>
        <name>Sheet_1  4/4</name>
        <job>
            <id>060671-21</id>
            <name>2021 Laramie High School</name>
        </job>
        <status>COMPLETED</status>
        <amountPlanned>544</amountPlanned>
        <wastePlanned>60</wastePlanned>
        <amountProduced>169</amountProduced>
        <wasteProduced>69</wasteProduced>
        <deviceId>114</deviceId>
        <types>ConventionalPrinting</types>
        <sequenceType>SheetfedPrinting</sequenceType>
        <start>2021-05-05T09:46:06-05:00</start>
        <end>2021-05-05T09:48:38-05:00</end>
        <startPlanned>2021-05-05T07:52:22-05:00</startPlanned>
        <endPlanned />
        <setuptimePlanned>0</setuptimePlanned>
        <prodtimePlanned>0</prodtimePlanned>
        <actualTimes />
    </worksteps>
    <worksteps>
        <id>_210505_073301714_027146</id>
        <name>Sheet_2  4/4</name>
        <job>
            <id>060671-21</id>
            <name>2021 Laramie High School</name>
        </job>
        <status>WAITING</status>
        <amountPlanned>0</amountPlanned>
        <wastePlanned>0</wastePlanned>
        <amountProduced>0</amountProduced>
        <wasteProduced>0</wasteProduced>
        <deviceId>XL106_Pool</deviceId>
        <types>ConventionalPrinting</types>
        <sequenceType>SheetfedPrinting</sequenceType>
        <start />
        <end />
        <startPlanned />
        <endPlanned />
        <setuptimePlanned>0</setuptimePlanned>
        <prodtimePlanned>0</prodtimePlanned>
        <actualTimes />
    </worksteps>
</job_worksteps>

--

TomD
  • 21
  • 3