# Converting data table to dictionary

**URL:** <https://forum.uipath.com/t/converting-data-table-to-dictionary/709990>\
**Category:** Studio\
**Tags:** studio\
**Created:** [January 3, 2024, 8:40am UTC](https://forum.uipath.com/t/converting-data-table-to-dictionary/709990 "2024-01-03T08:40:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harsha\_Vemula](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/harsha_vemula/32/221739_2.png) [@Harsha\_Vemula](https://forum.uipath.com/u/Harsha_Vemula)\
**Post date:** [January 3, 2024, 8:40am UTC](https://forum.uipath.com/t/converting-data-table-to-dictionary/709990/1 "2024-01-03T08:40:41Z")

</div>

Hi, I have a data table as below. Now I want to to convert it to dictionary in such a way that column name should be the Key and the below value should the value of the dictionary. Can someone help me on this.

| name | age | place | reason | sport |
| --- | --- | --- | --- | --- |
| rajesh | 30 | India | playing tournament | table tennis |

Example output :  
Key : name  
Value : rajesh

---

<div class="post-metadata">

**Author:** ![rlgandu](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/rlgandu/32/488436_2.png) [@rlgandu](https://forum.uipath.com/u/rlgandu)\
**Post date:** [January 3, 2024, 8:44am UTC](https://forum.uipath.com/t/converting-data-table-to-dictionary/709990/2 "2024-01-03T08:44:42Z")

</div>

@Harsha_Vemula

Read Range - Input: “YourFilePath”, Output: dtData

Assign - TypeArgument: Dictionary(Of String, String)  
To: dictData  
Value: New Dictionary(Of String, String)

For Each Row - Input: dtData  
For Each Column - Input: dtData.Columns  
Assign - To: dictData(Column.ColumnName)  
Value: row(Column).ToString  
End For Each Column  
End For Each Row

// Now, dictData contains the desired dictionary with column names as keys and corresponding cell values as values.

---

<div class="post-metadata">

**Author:** ![Sanjit\_Pal](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/sanjit_pal/32/193785_2.png) [@Sanjit\_Pal](https://forum.uipath.com/u/Sanjit_Pal)\
**Post date:** [January 3, 2024, 8:47am UTC](https://forum.uipath.com/t/converting-data-table-to-dictionary/709990/3 "2024-01-03T08:47:28Z")

</div>

@Harsha_Vemula have a look at this video

[![](https://global.discourse-cdn.com/uipath/original/4X/7/d/3/7d34fdcf7ae9f41d4da210696836c1747e38ce45.jpeg "LINQ | Convert Datatable To Dictionary | LINQ Uipath | Uipath Tutorials | Coderslobby") ](https://www.youtube.com/watch?v=Ne-nEt0FASw)

---

<div class="post-metadata">

**Author:** ![Anil\_G](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/anil_g/32/524291_2.png) [@Anil\_G](https://forum.uipath.com/u/Anil_G)\
**Post date:** [January 3, 2024, 8:49am UTC](https://forum.uipath.com/t/converting-data-table-to-dictionary/709990/4 "2024-01-03T08:49:27Z")

</div>

@Harsha_Vemula

Follow the steps

1. Create a dictionary variable dict, initialize with `New dictionary(Of String,String)` and datatable be dt
2. Now use for each activity with dt.Columns
3. Inside loop use assign activity with `dict(currentItem.ColumnName) = dt.Rows(0)(currentItem.ColumnName)`

Hope this helps

Cheers

---

<div class="post-metadata">

**Author:** ![mkankatala](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/mkankatala/32/484097_2.png) [@mkankatala](https://forum.uipath.com/u/mkankatala)\
**Post date:** [January 3, 2024, 8:55am UTC](https://forum.uipath.com/t/converting-data-table-to-dictionary/709990/5 "2024-01-03T08:55:50Z")

</div>

Hi @Harsha_Vemula

Try the following below code:  
Input:

 ![image](https://global.discourse-cdn.com/uipath/original/4X/4/8/0/480f53b79c69e75ad4b915f36035db192ef398f7.png)  
Use the below code in Invoke Code:

```auto
dataDictionary= New Dictionary(Of String, String)

For Each row As DataRow In YourDataTable.Rows
    For Each column As DataColumn In YourDataTable.Columns
        Dim key As String = column.ColumnName
        Dim value As String = row(column).ToString()

        ' Add the key-value pair to the dictionary
        dataDictionary.Add(key, value)
    Next
Next

' Now dataDictionary contains the data from the DataTable in dictionary format

```

Below are the invoked arguments:

 ![image](https://global.discourse-cdn.com/uipath/original/4X/5/6/3/5637ed3a44dc407dd2717d7ccba656e74ce9ea57.png)  
Workflow:  
 ![image](https://global.discourse-cdn.com/uipath/original/4X/c/3/5/c35952e3129b82921aaceaacbe6cf6b5e6cb0729.png)  
xaml:  
[Sequence11.xaml](https://forum.uipath.com/uploads/short-url/vDJaQHsdlNz6XifJcH61nd4fP7C.xaml) (9.0 KB)  
Output:  
 ![image](https://global.discourse-cdn.com/uipath/original/4X/1/7/d/17de5ec1b86175877ff537d13897e555314498d6.png)

Hope it helps!!  
Regards

---

<div class="post-metadata">

**Author:** ![pravallikapaluri](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/pravallikapaluri/32/484755_2.png) [@pravallikapaluri](https://forum.uipath.com/u/pravallikapaluri)\
**Post date:** [January 3, 2024, 9:49am UTC](https://forum.uipath.com/t/converting-data-table-to-dictionary/709990/6 "2024-01-03T09:49:11Z")

</div>

Hi @Harsha_Vemula

```auto
Read Range workbook

For Each Row activity:
   Input: yourDataTable

   For Each activity (nested inside For Each Row):
      Input: yourDataTable.Columns
      TypeArgument: DataColumn

      Assign activity (inside the nested For Each):
         To: outputDictionary(row.Table.Columns(column.ColumnName).ToString())
         Value: row(column).ToString()

```

 ![image](https://global.discourse-cdn.com/uipath/original/4X/1/e/b/1eb1c8cb1f1edb63875139c6add274f69ae0a18c.png)  
 ![image](https://global.discourse-cdn.com/uipath/original/4X/0/5/9/0591e71d38d48af60907ce3571ac8573d2e47199.png)

Hope it helps!!

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/system/32/182466_2.png) [@system](https://forum.uipath.com/u/system)\
**Post date:** [January 6, 2024, 9:49am UTC](https://forum.uipath.com/t/converting-data-table-to-dictionary/709990/7 "2024-01-06T09:49:12Z")

</div>

Closed on January 6, 2024, 9:49am UTC

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