Click here to Skip to main content
14,829,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using "System.Text.Json". JSON file contains multiple comma separated values, the code is only working for one block of Json Data. If I use the whole data, it does not parse and throws me an exception about '[' and ','. Which should not be the case, Am I missing something? Please see the below code and Json Data

Here's my code for the class:

VB
Public Class query
    Public Property RN As String
    Public Property QUERY_ID As String
    Public Property QTYPE As String
    Public Property SENDERNAME As String
    Public Property SENDEREMAIL As String
    Public Property SUBJECT As String
    Public Property DATE_RE As String
    Public Property DATE_R As String
    Public Property DATE_TIME_RE As String
    Public Property GLUSR_USR_COMPANYNAME As String
    Public Property READ_STATUS As Object
    Public Property SENDER_GLUSR_USR_ID As Object
    Public Property MOB As String
    Public Property COUNTRY_FLAG As String
    Public Property QUERY_MODID As String
    Public Property LOG_TIME As String
    Public Property QUERY_MODREFID As Object
    Public Property DIR_QUERY_MODREF_TYPE As Object
    Public Property ORG_SENDER_GLUSR_ID As Object
    Public Property ENQ_MESSAGE As String
    Public Property ENQ_ADDRESS As String
    Public Property ENQ_CALL_DURATION As Object
    Public Property ENQ_RECEIVER_MOB As Object
    Public Property ENQ_CITY As String
    Public Property ENQ_STATE As String
    Public Property PRODUCT_NAME As String
    Public Property COUNTRY_ISO As String
    Public Property EMAIL_ALT As String
    Public Property MOBILE_ALT As String
    Public Property PHONE As Object
    Public Property PHONE_ALT As Object
    Public Property IM_MEMBER_SINCE As Object
    Public Property TOTAL_COUNT As String
End Class
Main Form code:
VB
Imports System.IO
Imports System.Text.Json
Imports System.Text.Json.Serialization

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim json As String = 
        File.ReadAllText("~\test2.txt")
        Dim feed = JsonSerializer.Deserialize(Of query)(json)
        MsgBox(feed.ENQ_MESSAGE)
    End Sub

End Class
Here's the JSON Data
JavaScript
[
    {
        "RN": "1",
        "QUERY_ID": "1519852833",
        "QTYPE": "W",
        "SENDERNAME": "Name1",
        "SENDEREMAIL": "xyz@gmail.com",
        "SUBJECT": "Requirement for Black PP Granules",
        "DATE_RE": "30 Sep 2020",
        "DATE_R": "30-Sep-20",
        "DATE_TIME_RE": "30-Sep-2020 11:34:46 PM",
        "GLUSR_USR_COMPANYNAME": "Company Name1",
        "READ_STATUS": null,
        "SENDER_GLUSR_USR_ID": null,
        "MOB": "+91-1111111111",
        "COUNTRY_FLAG": "",
        "QUERY_MODID": "DIRECT",
        "LOG_TIME": "20200930233446",
        "QUERY_MODREFID": null,
        "DIR_QUERY_MODREF_TYPE": null,
        "ORG_SENDER_GLUSR_ID": null,
        "ENQ_MESSAGE": "My Requirement is for Black PP Granules.",
        "ENQ_ADDRESS": "Address1",
        "ENQ_CALL_DURATION": null,
        "ENQ_RECEIVER_MOB": null,
        "ENQ_CITY": "Panipat",
        "ENQ_STATE": "Haryana",
        "PRODUCT_NAME": "Black PP Granules",
        "COUNTRY_ISO": "IN",
        "EMAIL_ALT": "xyz11@gmail.com",
        "MOBILE_ALT": "+91-11111111111",
        "PHONE": null,
        "PHONE_ALT": null,
        "IM_MEMBER_SINCE": null,
        "TOTAL_COUNT": "178"
    },
    {
        "RN": "2",
        "QUERY_ID": "1519834488",
        "QTYPE": "W",
        "SENDERNAME": "Name2",
        "SENDEREMAIL": "xyz2@gmail.com",
        "SUBJECT": "Requirement for SHRILON Nylon  Granules",
        "DATE_RE": "30 Sep 2020",
        "DATE_R": "30-Sep-20",
        "DATE_TIME_RE": "30-Sep-2020 11:04:12 PM",
        "GLUSR_USR_COMPANYNAME": "Company Name2",
        "READ_STATUS": null,
        "SENDER_GLUSR_USR_ID": null,
        "MOB": "+91-22222222222",
        "COUNTRY_FLAG": "",
        "QUERY_MODID": "DIRECT",
        "LOG_TIME": "20200930230412",
        "QUERY_MODREFID": null,
        "DIR_QUERY_MODREF_TYPE": null,
        "ORG_SENDER_GLUSR_ID": null,
        "ENQ_MESSAGE": "Requirement2",
        "ENQ_ADDRESS": "Address2",
        "ENQ_CALL_DURATION": null,
        "ENQ_RECEIVER_MOB": null,
        "ENQ_CITY": "Chennai",
        "ENQ_STATE": "Tamil Nadu",
        "PRODUCT_NAME": "SHRILON Nylon  Granules",
        "COUNTRY_ISO": "IN",
        "EMAIL_ALT": null,
        "MOBILE_ALT": "+912222222",
        "PHONE": null,
        "PHONE_ALT": null,
        "IM_MEMBER_SINCE": null,
        "TOTAL_COUNT": "178"
    }
]


What I have tried:

I have tried confirming Json Data format. Json is correct and valid (RFC8259). To get this working, I have to remove '[]' and use just one block of data within '{}'. If I use the whole data, it does not parse and throws me an exception about '[' and ','.

I have also tried Newtonsoft Json and experienced the same problem.
Posted
Updated 3-Oct-20 22:58pm
v3
Comments
Sandeep Mewara 4-Oct-20 4:25am
   
2 things:
1. You validated the entire json and not just one block that JSON formatting is good. If not, would suggest use any online tool to quickly see if the entire json is a valid json. (https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwi30fuqwZrsAhWD7HMBHVNMBRkQFjAAegQIAhAC&url=https%3A%2F%2Fjsonlint.com%2F&usg=AOvVaw1wm0AnA86fJrIoZRGRzp6P)
2. It would help if you share the exact error you get and any stack trace for reference.
MaverickSingh 4-Oct-20 4:51am
   
1. I have validated the whole JSON, and not just a block. the result returns a valid json.
2. here's the error : System.Text.Json.JsonException: 'The JSON value could not be converted to ExportToExcel.query. Path: $ | LineNumber: 0 | BytePositionInLine: 1.'
Richard MacCutchan 4-Oct-20 4:29am
   
What is the exact message? Looking at your data there appears to be a missing item before the opening square bracket, something like "{"mydata":"which indicates an array of JSON sets.
MaverickSingh 4-Oct-20 4:52am
   
System.Text.Json.JsonException: 'The JSON value could not be converted to ExportToExcel.query. Path: $ | LineNumber: 0 | BytePositionInLine: 1.'
MaverickSingh 4-Oct-20 4:53am
   
this is a JSON return by an API. I have check the whole Json Data on https://jsonformatter.curiousconcept.com/ and it says that the json is valid (RFC8259)
MaverickSingh 4-Oct-20 4:54am
   
Here's the full error:

System.Text.Json.JsonException
HResult=0x80131500
Message=The JSON value could not be converted to ExportToExcel.query. Path: $ | LineNumber: 0 | BytePositionInLine: 1.
Source=System.Text.Json
StackTrace:
at System.Text.Json.ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type propertyType)
at System.Text.Json.JsonSerializer.HandleStartArray(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& state)
at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)
at System.Text.Json.JsonSerializer.ReadCore(Type returnType, JsonSerializerOptions options, Utf8JsonReader& reader)
at System.Text.Json.JsonSerializer.Deserialize(String json, Type returnType, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
at ExportToExcel.Form1.Button1_Click(Object sender, EventArgs e) in H:\Dotnet_projects\ExportToExcel\Form1.vb:line 13
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at ExportToExcel.My.MyApplication.Main(String[] Args) in :line 81

This exception was originally thrown at this call stack:
[External Code]
ExportToExcel.Form1.Button1_Click(Object, System.EventArgs) in Form1.vb
[External Code]

1 solution

Well yes. What did you expect?
You are providing a JSON array of two data items, and telling it to deserialize them into a single instance:
Dim feed = JsonSerializer.Deserialize(Of query)(json)
If you tell it it's an array:
Dim feed = JsonSerializer.Deserialize(Of query())(json)
Then it'll work.
But your MsgBox will fail.

Please, stop using outdated VB code: MsgBox is for compatibility with legacy VB6 which died in 2004 ... use MessageBox.Show instead.
   
Comments
MaverickSingh 4-Oct-20 5:24am
   
Thank you. It worked. Message box is just to see the data, I'll be converting it into Excel file.
OriginalGriff 4-Oct-20 6:30am
   
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900