-2

I am absolutely new in Json stuff, following is one of my returned JSON string from one of the Internal REST API Call:

{
   "odata.metadata":"https://ABC.XYZ.com/ERP10TESTNSSO/api/v1/Erp.BO.ABCCodeSvc/$metadata#Epicor.RestApi.ABCCodes/@Element",
   "Company":"93100",
   "ABCCode1":"A",
   "CountFreq":1,
   "ExcludeFromCC":false,
   "StockValPcnt":"0",
   "PcntTolerance":"0.00",
   "CalcPcnt":false,
   "CalcQty":false,
   "CalcValue":false,
   "QtyTolerance":"0",
   "ValueTolerance":"0",
   "ShipToCustNum":0,
   "SysRevID":"6066188743",
   "SysRowID":"5b7e2172-7f7a-445e-983d-f230e050153d",
   "BitFlag":0,
   "RowMod":""
}

Please let me know, how may I convert it into C# Data Table?

I tried following code (posted by another kind techie)

dynamic jsonObject = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonContent);
DataTable dt = JsonConvert.DeserializeObject<DataTable>(Convert.ToString(jsonObject.Value));

But this code is throwing error at jsonObject.Value.

Please help me to fix it ...

3
  • Using Json.Net you can deserialize directly to a data table... However, your json only contain a single object, not a json array, so I'm not sure that deserializing it to a data table would be the correct choice here... Commented Aug 19, 2020 at 8:22
  • Every time you use dynamic some part of the world dies a sad lonely death... (with random unhelpful errors) Commented Aug 19, 2020 at 8:24
  • you need to do several steps, first of all, read about Json, the main problem that you have got is that dt sees columns, among other things, and you are throwing data, without naming it, it does not make any sense. Secondly, define a class where the properties are the name of the Json properties, then create the object and deserialize, I am sure that you can work out how to transform from an object to a datatable Commented Aug 19, 2020 at 8:25

1 Answer 1

0

Try this:

 string json = "{\"odata.metadata\":\"https://ABC.XYZ.com/ERP10TESTNSSO/api/v1/Erp.BO.ABCCodeSvc/$metadata#Epicor.RestApi.ABCCodes/@Element\",\"Company\":\"93100\",\"ABCCode1\":\"A\",\"CountFreq\":1,\"ExcludeFromCC\":false,\"StockValPcnt\":\"0\",\"PcntTolerance\":\"0.00\",\"CalcPcnt\":false,\"CalcQty\":false,\"CalcValue\":false,\"QtyTolerance\":\"0\",\"ValueTolerance\":\"0\",\"ShipToCustNum\":0,\"SysRevID\":\"6066188743\",\"SysRowID\":\"5b7e2172-7f7a-445e-983d-f230e050153d\",\"BitFlag\":0,\"RowMod\":\"\"}";

 json = $"[{json}]";

 DataTable dt = JsonConvert.DeserializeObject<DataTable>(json);

Add [ ] in your json string.

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.