Click here to Skip to main content
15,883,835 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)

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.
 
Share this answer
 
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