Click here to Skip to main content
15,897,334 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I need to create web service web api and i need to create CRUD and menu but I have the data as json file , I need to Convert the data from file.json to datatable in sql server and convert the data from sql server to file .json automatically . because when the data become as data in tabe than it will be easy to create CRUDE in MVC web api C#
Is any one has any idea , please help

What I have tried:

Nothing.
Posted
Updated 12-Apr-18 23:54pm
v2
Comments
faayez 13-Apr-18 3:05am    
Please any link , Idea ,
F-ES Sitecore 13-Apr-18 4:56am    
You can't convert json to a sql table as a table is a number of rows of a set column. How would you convert this json to a table?

{
name: "John":,
phones: [{mobile:"079004465", office:"0123 456489"}]
workHistory: [{company:"Company 1", positions[{"Junior Developer", "Senior Developer"]]
}

What you have to do is look at the json you have, work out how to represent that json as tables and then write code to put that json into tables, and then code to convert from the data back to json. Google for "json serialisation" to see how you can do some of that work easier.

1 solution

Deserialize your json collection:

Deserialize a Collection[^]

After that, write a method that converts your deserialized objects into a datatable, and Bob's your uncle.

BTW, if you use the netwtonsoft json.net library (a nuget package), you can deserialize directly to a datatable.

C#
DataTable dt = (DataTable)JsonConvert.DeserializeObject(myJsonString, (typeof(DataTable)));


The only possible issue is that it will deserialize all of the fields as string object.
 
Share this answer
 
v4

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