Complex JSON Serialization

I am trying to serialize a complex json. There are arrays within arrays, arrays of objects, etc.

From what I have found through these forums, there are ways to serialize arrays, but doesn’t go into more complex configurations. Ideally I could build a vb.net object and serialize it, however I don’t see that is possible with the out of the box invoke code activity. Is there any other way I could accomplish this?

Thank you!

Edit: I am trying to go from individual data points → JSON string. Not the other way around. From comments I’m getting maybe that was unclear.

You could build a custom activity that takes in your json and outputs a serialized string

Hi @Greer,

Whatever may be the complexity you can achieve this by the existing JSON activities in UiPath.

Have you tried with those?

Just to clarify - I am trying to go from individual string data points into a JSON formatted string. I am using JsonConvert.SerializeObject(), but that only seems to be handle a single array.

Hi @Greer

Are you attempting to serialize a multi-dimensional array?

No, whatever we can do with JSON in code, we can do that with the existing activities, If it has multidimensional array also.

This is an example of the json I need to create:
{

“personalizations”: [

{ “to”: [

{ “email”: “test@domain.com”, “name”: “Recipient Name” }

],

“dynamic_template_data”: {

“URL”: “www.google.com”,

“refId”: “12345”,

“Amount”: “123.12”

}

}

],

“template_id”: “1231112”,

“from”:

{ “email”: “me@mycompany.com”, “name”: “Program Support” },

“reply_to”:

{ “email”: " me@mycompany.com ", “name”: " Program Support " },

“tracking_settings”:{ “open_tracking”: { “enable”: true } }

}

Fixed formatting:

{

    “personalizations”: [

        { 
            “to”: 
            [

                {
                    “email”: "test@domain.com", “name”: “Recipient Name” 
                }

            ],

            “dynamic_template_data”: 
            {

                “URL”: “www.google.com”,

                “refId”: “12345”,

                “Amount”: “123.12”

            }

        }

    ],

    “template_id”: “1231112”,

    “from”:

    { 
        “email”: "me@mycompany.com", “name”: “Program Support” 
    },

    “reply_to”:

    { 
        “email”: " me@mycompany.com ", “name”: " Program Support " 
    },

    “tracking_settings”:
    { 
        “open_tracking”: 
        { 
            “enable”: true 
        } 
    }

}

Do you have examples of the individual data pieces you input to this?

I will likely be using a datatable for the “to”, and a dictionary for the “dynamic_template_data”, “from”, “reply_to”, all else will be primitive data types

@Greer

Are the dictionaries as such:

Dictionary<string, string>

Yep! exactly

The best thing I can recommend is to build a custom activity that takes each item as input and outputs a serialized json string.

These are good resources for building one:

1 Like

@Greer

Not sure if you still need a solution, but I attempted to make an activity for this.
SerializeDataCustomActivity.1.0.0.nupkg (5.7 KB)

If you could try it out and let me know if you run into errors, that’d be greatly appreciated.

1 Like

I am having a hard time being able to open up and view that attachment. Can you please post the project.json or xaml?

Can you please explain which activities I can use?

Hi Greer,

The attachment I posted is a nuget package. To use it in your project, add it to the local dependency folder for UiPath. Then in manage packages, add it to the project.

1 Like

That is incredibly helpful, thank you! @nlee1131 Looks like I’ll need to make a package then.

Here’s an example of a custom activity
Serialize.cs (2.1 KB)

This is within a Class Library (.NET Framework) in Visual Studio

2 Likes

Here is the workflow to extract data from the JSON you provided @Greer ,

jsonComplex.zip (3.9 KB)

For every value from the json you want, you can get it with the key values

1 Like

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