Converting Dictionary or Json Object to datatable row

Hi Experts

Is it somehow possible to convert a Dictionary Of String, Object to a row and add it to a data table? Each key in the dictionary represents the data column name.

Alternative if I convert the dictionary to a JObject can it then be parsed to a data table row?

Newtonsoft.Json.JsonConvert.SerializeObject(myDictionary)

The result of the JObject will look like:

{"ID":1,"Submitted":"2023-01-24T08:14:50.3847132Z","Email":"noreply@noreply.com","Name":"John Doe","Readiness":"Ready now (RN)","Potential":"High","Performance":"1"}

As I am building a library it should also work dynamically as only the ID, Submitted, Email and Name are static. But for each process there might be other attributes so the columns of the data table should be populated dynamically.

give a try at

Include Newtonsoft.Json, Newtonsoft.Json.LINQ to the imports (panel close to the variable panel)

Assign activity
dtResult =
JArray.FromObject( { YourStringObjectDictVar } ).toObject(Of DataTable)

E.g

Hi @ppr

I am getting the following error:

Assign: Object serialized to Object. JArray instance expected.

Any idea why that is?

share some details with us e.g screenshots from the modelling and variables

I have created a Dictionary Of String, Object:
jsonDictionary | New Dictionary (Of String, Object)

Added values to the dictionary:
jsonDictionary(“ID”) | response(“id”)
jsonDictionary(“Submitted”) |response(“submitDate”)
jsonDictionary(“Email”) | response(“responder”)
etc.

I have then create a new data table:
responseDt | New DataTable

And then I am trying to populate the data table:
responseDt | JArray.FromObject(jsonDictionary).ToObject(Of DataTable)

we shared a different code line

kindly note the { } brackets
or to be more conform with target framework: windows

JArray.FromObject( new Dictionary(of String, Object)(){ YourStringObjectDictVar } ).toObject(Of DataTable)

1 Like

I was missing out the brackets, sorry - it is working now. Thank you very much!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.