0
{
      "Company": "XXXX",      
      "Employee": {
        "Id":{
          "Value": 0011
        },  
        "Name": "Sankar"        
      }
      "Myids": "sdfsdf-sdf-sdf-sdf-sdf", 
} 

if there is nested json , am getting error " Unexpected JSON token when reading DataTable: StartObject. Path 'Devices[0].LocationGroupId', line 11, position 26", otherwise it works fine.

   StreamReader stRead = File.OpenText(@"C:\Users\ssankare\Desktop\json.txt");
      string json = stRead.ReadToEnd();

      var dataSet = JsonConvert.DeserializeObject<DataSet>(json);
      var table = dataSet.Tables[0];
Sankar S
  • 21
  • 6
  • The first very obvious thing to note is that XML is not JSON. It's unclear why you are trying to use XML classes to deal with JSON data. Also, why do you want to create a `DataTable` specifically? What do you plan to do with this data, eventually? Maybe there is a better way to process it. – ADyson Dec 02 '19 at 11:36
  • But if you really need a `DataTable` for some reason, then please see https://stackoverflow.com/questions/11981282/convert-json-to-datatable . The answers are in C# but you can easily convert the code to VB.NET yourself, or using an online converter. Did you do any research into your problem? – ADyson Dec 02 '19 at 11:38
  • Thanks for your reply. If it is having nested json getting error, otherwise it works fine." Unexpected JSON token when reading DataTable: StartObject. Path 'Devices[0].LocationGroupId', line 11, position 26" – Sankar S Dec 02 '19 at 12:27
  • That's to be expected, I think. After all, a nested structure implies a hierarchy. But a table is a flat structure. So how should the code know what to do with the nested data? It can't. You would need to give explicit instructions. But like I said, what are you trying to do with this data eventually? I assume it goes somewhere else after you put it in the data table? What will you be using it for? Perhaps we can go directly there from the JSON, without needing a datatable. – ADyson Dec 02 '19 at 12:49
  • @ADyson, Thanks .I want to write it to CSV in the following format Company,EmployeeId,employeename,Myids XXXX,0011,Sankar,sdfsdf-sdf-sdf-sdf-sdf – Sankar S Dec 02 '19 at 13:04
  • Ok well you certainly don't need a datatable for that, although it is one possible way to achieve it. A simple array of strings would be sufficient. But either way you need some code to convert the hierarchical structure of your JSON into the flat structure of a table. That can't happen automatically, because the computer doesn't know the rules to apply. You probably need to use the JArray, JObject etc. classes in the JSON.NET library to manually traverse the JSON and write the values to the correct place in your datatable or array. – ADyson Dec 02 '19 at 13:08
  • 1
    Either that or, if you control the program which creates the JSON, then alter it to have the ability to output a flat JSON structure – ADyson Dec 02 '19 at 13:09
  • How to use Jarray or jobject for my case ,can you share any example code, if any. Thanks in advance.. – Sankar S Dec 02 '19 at 16:54

0 Answers0