1

I have an open SqlConnection (extended by Dapper), a table name in the connected database, and a JSON string that I trust to be of the same schema. What's the simplest way to insert the JSON object's field values without deserializing to a static type?

I realize the standard option is to create a class representing the record to deserialize into. However, there are several reasons this is less than ideal. I'm syncing a number of tables in exactly the same way. The schema already exists in two places (the source and the target), so it seems poor form to repeat the schema in the middleware as well. Also, since I'm just going straight into the database, it seems excessive to require a recompile any time someone adds an additional column or table.

Is there a more dynamic solution?

2
  • This might be of interest to you Commented Nov 12, 2013 at 1:24
  • That is interesting. Unfortunately the project there stores the result in a generic JSON Hierarchy table. I'm looking to insert into a particular table with the column names matching the JSON object properties. I could probably get there from here, but it would require some pretty ugly SQL. Commented Nov 12, 2013 at 13:39

2 Answers 2

2

probably deserializing into a dynamic is your best bet. if you want to pull out the values directly from the string, you're going to have to (at least partially) parse it anyways, and at that point you might as well just deserialize it

See this answer for an example using JSON.net: Deserialize json object into dynamic object using Json.net

Sign up to request clarification or add additional context in comments.

Comments

0

Deserialize into a dictionary, then construct Dapper DynamicParameters.

How to create arguments for a Dapper query dynamically

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.