Results 1 to 7 of 7

Thread: JSON nested array - retrieving values

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    117

    JSON nested array - retrieving values

    I’m having difficulty extracting data from a particular JSON response which has nested arrays. I can easily access the first level data, but not the second.

    My declarations are:
    Code:
    Public Class GPRResponse
            Public Property participant As IEnumerable(Of participant)
        End Class
        Public Class participant
            Public Property name As String
            Public Property id As String
            Public Property serviceGPR As IEnumerable(Of serviceGPR)
        End Class
        Public Class serviceGPR
            Public Property service As String
        End Class
    And the request:
    Code:
    Dim responseObject = JsonConvert.DeserializeObject(Of GPRResponse)(resultGPRreport)
    For Each participant In responseObject.participant
                    m1 = participant.name
                    m2 = participant.id
                    For Each service In participant.serviceGPR
                        m3 = participant.serviceGPR
                        logEvent("serviceGPR: " & m3, mw.activateOutputTB)
                    Next
                Next
    And a typical responsedata content is:
    Code:
    {  "participant" : [ {
        "name" : "Credicare Fund Limited",
        "id" : "CHF",
        "service" : [ "Service1", "Service2", "Service3", "Service4" ]
      } ]
    }
    So I can retrieve the values for iterations through name and id but not service, with the For Each service line return a null reference error despite the responseObject containing all the data for this element. Logically, it seems right to define m3 as m3 = participant.service, but this returns an error that service is not a member of participant.

    What am I doing wrong?

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: JSON nested array - retrieving values

    Quote Originally Posted by Bear89 View Post
    I’m having difficulty extracting data from a particular JSON response which has nested arrays. I can easily access the first level data, but not the second.

    My declarations are:
    Code:
    Public Class GPRResponse
            Public Property participant As IEnumerable(Of participant)
        End Class
        Public Class participant
            Public Property name As String
            Public Property id As String
            Public Property serviceGPR As IEnumerable(Of serviceGPR)
        End Class
        Public Class serviceGPR
            Public Property service As String
        End Class
    And the request:
    Code:
    Dim responseObject = JsonConvert.DeserializeObject(Of GPRResponse)(resultGPRreport)
    For Each participant In responseObject.participant
                    m1 = participant.name
                    m2 = participant.id
                    For Each service In participant.serviceGPR
                        m3 = participant.serviceGPR
                        logEvent("serviceGPR: " & m3, mw.activateOutputTB)
                    Next
                Next
    And a typical responsedata content is:
    Code:
    {  "participant" : [ {
        "name" : "Credicare Fund Limited",
        "id" : "CHF",
        "service" : [ "Service1", "Service2", "Service3", "Service4" ]
      } ]
    }
    So I can retrieve the values for iterations through name and id but not service, with the For Each service line return a null reference error despite the responseObject containing all the data for this element. Logically, it seems right to define m3 as m3 = participant.service, but this returns an error that service is not a member of participant.

    What am I doing wrong?
    IIRC the class names need to match up with the JSON, so "serviceGPR" should be "service", although you could also use https://www.newtonsoft.com/json/help...Attributes.htm to override these defaults (if you are using newtonsoft that is)

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    117

    Re: JSON nested array - retrieving values

    Hmm, I tried that before, and have gone back to it as this:
    Code:
        Public Class GPRResponse
            Public Property participant As IEnumerable(Of participant)
        End Class
    
        Public Class participant
            Public Property name As String
            Public Property id As String
            Public Property service As IEnumerable(Of Service)
        End Class
        Public Class serviceGPR
            Public Property service As String
        End Class
    But then I get a deserialisation error with the following:
    Code:
    23/05/2022 8:16:19 PM : service: Newtonsoft.Json.JsonSerializationException: Error converting value "In Hospital Claim" to type 'Goldie.JSONUtility+Service'. Path 'participant[0].service[0]', line 5, position 37. ---> System.ArgumentException: Could not cast or convert from System.String to Goldie.JSONUtility+Service.
       at Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value, Type initialType, Type targetType)
       at Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue, CultureInfo culture, Type targetType)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)
       --- End of inner exception stack trace ---
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
       at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
       at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
       at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
       at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)
       at Goldie.DeviceServices.GPR() in C:\Esyclaims\Goldie\DeviceServices.vb:line 864

  4. #4
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: JSON nested array - retrieving values

    Your class is still declared as
    Code:
    Public Class serviceGPR
        Public Property service As String
    End Class
    Yet the property is defines as
    Code:
    Public Property service As IEnumerable(Of Service)
    The property is not referring to your class, you need to change your class name to service or use the attributes in the link I included to override the default names.

  5. #5
    Fanatic Member
    Join Date
    Jun 2019
    Posts
    557

    Re: JSON nested array - retrieving values

    Actually it is list of strings so definition should be:
    VB.NET Code:
    1. Public Property service As IEnumerable(Of String)

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    117

    Re: JSON nested array - retrieving values

    Thanks Peter, perfect, now deserialises, but I still can't get the value -

    Code:
    23/05/2022 9:13:26 PM : Name: Credicare Fund Limited
    
    23/05/2022 9:13:26 PM : Id: CHF
    
    23/05/2022 9:13:26 PM : Service: System.Collections.Generic.List`1[System.String]

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    117

    Re: JSON nested array - retrieving values

    Ah, I see it now, I just need to specify the index - eg m3 = participant.service(1)

    Thanks guys.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width