0

I've been searching high and low, and can't find a resolution to this so would welcome some guidance please. Spoiler - working in VB, not C# :-)

I have the following JSON data:

[{
    "payload": {
        "MainBlock": {
            "S0": {
                "interfaceId": "IF-XXX",
                "schemaVersion": "1.1.1",
                "eventCode": "[ABCPeriodData]"
            },
            "S1": {
                "environment": "DEV",
                "senderUniqueReference": "S-005-1234567abc-SUP-20222313-12345687A",
                "sentTimestamp": "2022-12-31T12:35:45\u002B06:30",
                "senderId": "4ce0a6cd4b",
                "senderRole": "ABC",
                "DIPConnectionProviderId": "1009012345"
            },
            "A0": {
                "always": ["SUP", "SDS", "ADS", "UMDS"]
            },
            "D0": {
                "transactionId": "T-022-4ce0a6cd4b-ABC-20230503-000404",
                "transactionTimestamp": "2023-05-03T13:34:34\u002B00:00",
                "publicationId": "PUB-XXX"
            }
        },
        "SecondBlock": {
            "B903List": [{
                "calculationDay": {
                    "calculationDayDate": "2022-12-31T12:35:45\u002B06:30",
                    "calculationPeriodDuration": 30
                },
                "XYZcalculationPeriods": [{
                    "XYZroup": {
                        "GSPGroupID": "_K",
                        "connectionTypeIndicator": "W",
                        "domesticPremiseIndicator": true,
                        "marketSegmentIndicator": "A",
                        "measurementQuantityID": "AI"
                    },
                    "XYZroupcalculationPeriods": [{
                        "calculationPeriodEffectiveToDateTime": "2022-12-31T12:35:45\u002B06:30",
                        "loadShapePeriodValue": "123456789.123",
                        "defaultLoadShapeFlag": "A"
                    }, {
                        "calculationPeriodEffectiveToDateTime": "2022-12-31T12:35:45\u002B06:30",
                        "loadShapePeriodValue": "123456789.123",
                        "defaultLoadShapeFlag": "A"
                    }]
                }]
            }]
        }
    }
}]

Using JSONLINT this validates as valid JSON data.

Trying to parse using Newtonsoft. I have already parsed a downloaded file successfully, and the data above is one of the values sent in an enclosing JSON file, so I know that parse method I have seems to work.

If I use the same method to parse the JSON above, as I did to parse the original download, it fails

Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.

Using the following in a second parse method as the first one doesn't work on this. It would seem that I need to approach this particular type of JSON data differently.

            Dim o As JObject = JObject.Parse(Json)
            Dim results As List(Of JToken) = o.Children().ToList
            For Each item As JProperty In results
                item.CreateReader()
                If item.Value.Type = JTokenType.Array Then
                    Dim results2 As List(Of JToken) = item.Value.ToList
                    For Each subitem As JObject In results2
                        WorkingDictionary = New Dictionary(Of String, String)
                        Dim results3 As List(Of JToken) = subitem.Children().ToList
                        For Each temp2 As JProperty In results3
                            temp2.CreateReader()
                            Try
                                Debug.Print(temp2.Name)

                                WorkingDictionary.Add(temp2.Name, temp2.Value.ToString)

                                'Debug.Print(temp2.Name)
                                'Debug.Print(temp2.Value.ToString)
                            Catch ex As Exception
                                Debug.Print(ex.StackTrace)
                                Debug.Print(ex.Message)
                            End Try

                        Next
                        RetVal.Add(WorkingDictionary)
                    Next
                Else
                    Debug.Print(item.Name.ToString + "   " + item.Value.ToString)
                End If
            Next
            '         Next
        Catch ex As Exception
            Debug.Print(ex.StackTrace)
            Debug.Print(ex.Message)
        End Try

I've already looked at Newtonsoft.Json: Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray and none of them work.

Guidance welcome please, as parsing JSON is completely new to me.

2
  • Issue might be that the top-level item is an array and not a scalar? Commented May 4, 2023 at 15:07
  • You're saying For Each item As JProperty In results and then trying to use item as the JsonReader? I'm not sure about the parsing of the JObject; that looks fine to me. But maybe you want to change item.CreateReader() into something like Dim reader As JsonReader = item.CreateReader(). Commented May 4, 2023 at 19:20

1 Answer 1

0

There was a previous post from Serge which I was going to mark as the answer, but it's been deleted.

The code section should start with

Dim o As JArray = JArray.Parse(Json)

This is then the basis of parsing further as necessary.

If @serge would like to repost their original comment, I'd gladly mark as the accepted answer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.