Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the first JSON string:
{
    "ContractID": 140,
    "EndpointIdentifier": "24.2452",
    "ErrorMessage": "",
    "LogFileCollection": [
        "C:/Gaston/GrandScheme/MicroLog4.txt",
        "C:/Gaston/Grandscheme/MicroLog2.txt"
    ],
    "MethodName": "TestModeExit",
    "OperationBegan": "/Date(092120-0400)/",
    "OperationCompleted": "/Date(092120-0400)/",
    "OperationID": 1,
    "Outcome": 0,
    "Parameters": [
        {
            "Name": "ESPN",
            "Value": "23.4.39447",
            "VariableType": "System.String"
        }
    ],
}

And my second JSON file:
{
    "CellRelayESN": "VR-[26::AB::]",
    "CellRelaySerialNumber": "[26::AB::]",
    "ContractID": 0,
    "DeviceClassName": "GastonGrand",
    "EndpointIdentifier": "2.3532.432",
    "ErrorMessage": "",
    "IsAuthenticated": true,
    "IsRegistrationProcessComplete": false,
    "LastDeregistered": "/Date(092120-0500)/",
    "LastRead": "/Date(092120-0500)/",
    "LogFileCollection": [],
    "MethodName": "RegistrationInformation",
    "OperationBegan": "/Date(092120)/",
    "OperationCompleted": "/Date(092120)/",
    "OperationID": 2,
    "Outcome": 0,
    "Parameters": [
        {
            "Name": "jobManager",
            "Value": "jobFoundYesOrNo",
            "VariableType": "String"
        },
        {
            "Name": "ESPN",
            "Value": "2.16.839447",
            "VariableType": "String"
        }
    ],
    "ReturnType": "RegistrationInfoResult",
    "Server": "Host-1",
    "ServiceTypeID": 1,
    "Status": "Registered",
    "TestRunID": 3206,
    "TokenID": null
}


What I have tried:

Currently I am storing these two JSON files into one single columned DataTable that I later convert into a string to be parse with JToken.Parse. However, when I do try to parse it, I encounter the error:
Additional text encountered after finished reading JSON content: {. Path '', line 2, position 0.'

at the
JToken token = JToken.Parse(table);
line. I am wondering, how do I correctly combine these two JSON files into the same string so it can be correctly parse? Here's the rest of my code:

public static List<string> CleanOperationsTable(string liteConString)
        {
            List<string> cleanedList = new List<string>();
            SQLiteConnection liteCon = new SQLiteConnection(liteConString);
            liteCon.Open();
            DataTable dtbl = new DataTable();

            string select = "SELECT ResponseBody FROM Operations LIMIT 2";

            SQLiteCommand cmd = new SQLiteCommand(select, liteCon);
            SQLiteDataAdapter sqliteDAP = new SQLiteDataAdapter(cmd);
            sqliteDAP.Fill(dtbl);

            /// Converts the 1 column DataTable into a single string to 
            /// it's easier to parse with JSON
            string table = string.Join(Environment.NewLine, dtbl.Rows.OfType<DataRow>().Select(x => string.Join(" ; ", x.ItemArray)));

            JToken token = JToken.Parse(table); // ERROR HERE


            return cleanedList;
        }
Posted
Updated 22-Sep-20 4:31am
Comments
Richard MacCutchan 22-Sep-20 9:31am    
You should print out the combined text to ensure it is correctly formatted.
stevenlam505 22-Sep-20 9:35am    
I believe I encounter an error because of the leading and ending curly braces in each of them. Do you know how I could remove them?
Richard MacCutchan 22-Sep-20 9:43am    
You need to look at what you are doing with the data that you extract from your database. What you suggest could be true, but you need to examine the actual data to be certain.

1 solution

Try something like this:
C#
var rows = dtbl.Rows.Cast<DataRow>().Select(r => Convert.ToString(r[0]))
JArray token = new JArray(rows.Select(r => JToken.Parse(r)));
 
Share this answer
 
v2
Comments
stevenlam505 22-Sep-20 10:39am    
I assume that I'm supposed to replace the JToken line with that code, but when I do, I receive this error: Severity Code Description Project File Line Suppression State
Error CS1929 'DataRow' does not contain a definition for 'GetString' and the best extension method overload 'DataReaderExtensions.GetString(DbDataReader, string)' requires a receiver of type 'DbDataReader' SQLiteExtractor C:\Users\MainUser\source\repos\SQLiteExtractor\SQLiteExtractor\Program.cs 140 Active
Richard Deeming 22-Sep-20 10:41am    
Try Convert.ToString(r[0]) instead of r.GetString(0).
stevenlam505 22-Sep-20 10:47am    
That works, thanks so much! Could you explain what this code does?
stevenlam505 22-Sep-20 10:40am    
Here's what it looks like https://imgur.com/a/Gk8Ot4o

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