Covert text to json

Hello

Im trying to serialize my text “{SolutionId:123123,ClientId:123123}”

to Json format
but after using newtonsoft i get !

“"{SolutionId:123123,ClientId:123123}"”

what is the pest practice to build json object and serialize it ?

i did try to write to text file but no luck :exclamation:
Im using C# any tips

BR
Hazem

Hi @Hazem_Saleh,

Use the Build JSON Object activity to create a JSON object with your desired properties. In your case, it would look something like this:

{
  "SolutionId": 123123,
  "ClientId": 123123
}

Thanks,

Hi,

How about the following?

JsonConvert.SerializeObject(jo,Newtonsoft.Json.Formatting.Indented)

Regards,

To explain why you’re having a problem…

{SolutionId:123123,ClientId:123123}

…is not valid JSON. You’d need single quotes around SolutionId and CilentId. The solutions provided account for this.

1 Like

This is already serialized.
I think you want to Deserialize your text.

(This is also assuming you also fix the issues regarding quotes raised by RajKumar, Yoichi and Paul)

Thanks, but then i have to add to http as string! ?
im trying invoke code but seems not happy when i input the string for the Json converter !

Thanks everyone

You don’t want to use Invoke Code. You want to use an Assign acitivity to put the result into a variable.

I mean Paul, he’s got it backwards anyway. Yoichi showed deserializing a string to a JObject (jo) and then how to serialize the JObject.

There is a fundamental misunderstanding here by Hazem where he thinks he can/should serialize a string. Thats not logical. Yoichis code for example demonstrates serializing and deserializing but you end up back where you start.
I think we need to clarify the misunderstanding before any meaningful process can continue.

1 Like

you are right I’m just building this string, but still always missing the “” when i send it via api…

What is the best way around it

i followed the official example
image

But now how i can pair them!
Or this is ok for Json body!

@Hazem_Saleh

What you created is a json array

If you need a jobject then use deserialize json activity and pass the string as below and output would be the jobject

"{""SolutionId"":""123123"",""ClientId"":""123123""}"

Cheers

Thanks,

how to format this im trying i got instead of {} should i add ( : ) as character ?

how to have obj.add with " !

I need it to look like {“SolutionId”:“123123”}

Im getting closer but very confused :stuck_out_tongue:

As I have been urging.
I think you need to stop for a minute and learn some fundamentals about what ‘Serializing’ and ‘Deserializing’ is and help explain to us what you are actually trying to achieve cause ‘Serializing a string’ makes no sense.

1 Like

@Hazem_Saleh

What you created is a jarray and not jobject

Easyway:

Invoke Code way:

Hope this is clear

cheers

1 Like

I’d like to also share an opinion. Avoid Invoke Code like the plague, its hard to debug. You very very rarely need to use it. I think I used it once due to a bug in the compiler.

You are right!
i dont think i need that but was trying to figure it
I have two variable solutionid and clienID
im trying to achieve same as this result How to serialize and deserialize JSON using C# - .NET | Microsoft Learn

{"solutionid ":“1212”,“ClientID”:“1211”}

but couldn’t do the same in UiPath

Is that the main goal, you just want to duplicate some C# functionality for learning purposes? I’d definitely not use Invoke Code in that case as that surely defeats the point.

As I’ve stated before, getting to understand that you don’t serialize a string is the first step.

when having 2 variables like
grafik

then we can create the JObject / serialized JSON String by
Option 1:
grafik

Option 2:
grafik

as also other options are possible. The shown snippets can be used within Assign Activities

C#:

myDict = new Dictionary<string,string>{{"SolutionID",strSolutionID},{"ClientID",strClientID}};
Newtonsoft.Json.Linq.JObject.FromObject(myDict);
Newtonsoft.Json.Linq.JObject.FromObject(myDict).ToString();

Thanks alot its seems my problem i quite dont understand where to find the right type
if i use assign myDict its going to be Dictionary but cant find the type under browse for all type !
same for the second one Newtonsoft.Json.Linq.JObject.FromObject(myDict);

Hazem

grafik
grafik
then click ok, ok

similar
grafik