Convert Json to Datatable without using loop

Hello,

I’m deserialize a json for JOBJECT and would like to transform this JOBJECT straight to data table without looping due to high amount of information.

Below the structure of my JSON, the data table information would only be the highlighted ones.

{
“name”: “relatorio”,
“elements”:[
{
"cod_ua": “01”,
"cod_carteira": “222”,
"cpf_cnpj": “99999999999”,
},
{
"cod_ua": “01”,
"cod_carteira": “222”,
"cpf_cnpj": “99999999999”,
}
],
“links”:[
{
“rel”: “self”,
“href”:
https://teste.com
}
]
}

1 Like

UiPath.WebAPI.Activities contains methods and activities for converting things to and from JSON.

you can simply parse it and convert to Datatable.

Below thread is quite helpful on this topic

Hi @joao_adelio1,

You can use below code to achieve the same.

This use build datatable activity and invoke code activity, its quite fast. No need to run thorugh loops as that would be time consuming.

Sequence.xaml (10.0 KB)

Just make changes as required(few mentioned below for your quick reference:)

  1. Change column names in build datatable activity to your column names.
  2. Do regex changes (change per column names) in matches activity. Also mention your json onbject under Input in properties panel of matches activity.
  3. Change the column names in invoke code activity.

And ta-da!!

You got your datatable :slight_smile:

REgards
Sonali

Thank you in advance for your help.

I tried to do as you told me, but my data table returns blank, I’ve attached the json and the code so you can see what I’m doing.

TESTE.json (2.5 KB)
Main.xaml (34.6 KB)

give a try on following

prepare a datatable with build datatable activity - dtData
All cols similar to all properties from the JObject in elements
parse the JSON string with Deserialize JSON activity - myJObject

Use an assign acitvity
left: dtData
right

(From x in myJObject("elements").Values(Of JObject)
let ra = x.Values.Cast(of Object).toarray
Select r=dtData.Rows.Add(ra)).CopyToDataTable

In case of you want to dynamize the datatable preparation you can loop over the first JObject properties or over an array with its names
grafik

Using this within a for each and combined with an add data column to dtData will reduce some manually effort

2 Likes

Thanks Peter,

can you help me on this other topic Transform datatable string column into data time format (DD-MM-YYYY)

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