Dictionary to datatable key as column name value as data

I have dictionary (key, value) need to convert that into datatable in way that all keys are datatable coulmns and all respective values are in row. For eg Dic = (id,1) (Name,abc)(age,25)…

Datadable should be
Id,name,age
1,abc,25

#help #question #datatable

Hi @Tate_S

Please follow the steps

Create a datatable variable dt and initialize New Datatable()
Create a string variable str

  1. Drag a for loop and in the in arguments give DictionaryName.Keys.ToArray and change the type argument to String
  2. Inside use a add data column and give the column name as currentitem type can be string for all
  3. Use assign activity str = If(IsNothing(str),dictionaryname(currentitem).ToString,str + "," + dictionaryname(currentitem).ToString)
  4. Out side the for loop use add data row and pass the array row values as str.Split({","},StringSPlitOptions.None).ToArray()

cheers

grafik

Assign Activity:
Left Side: dtData | DataType: DataTable
Right side: Newtonsoft.Json.Linq.JArray.FromObject({yourDictVar}).ToObject(Of DataTable)

1 Like

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