For Each with JSON Help

Hello,

My JSON skills aren’t the greatest. Looking for some help.

I am trying to go through each phone number that appears on this account, check if “unsubscribedModes: [SMS]” is present for a corresponding phone number. If so, then skip this transaction.

My issue is - I can’t get each individual phone number to show up in my “write line” activity. I can’t do “currentitem(“phoneNumber”).tostring” for some reason. I can only get my “write line” to return each individual object.

How can I make this so I can pull out whatever piece of info I need? ie. The phone number and if SMS is unsubbed.

Thank you

Hi @Josh_James ,

Could you maybe try performing a DirectCast on the currentItem instance, since it is recognised as Object type, maybe a type cast is required.

DirectCast(currentItem,JObject)("phoneNumber").ToString
1 Like

Hi @supermanPunch - Thank you, that worked! I would have never figured this out… I have older projects that let me simply put in (“item1”)(“item2”). No clue what “DirectCast” is, but thank you!!

Do you know of a cleaner way to get “SMS” to show up from the array in the output panel? There may be times when other strings are in that array along with “SMS”.

we hope that it is ensured that the for each typeargument was set to JObject
Can check and tell us the result? If so the direct cast should be obsolete

Similar, if a Deserialize JSON Activity is involved, check also always the configured TypeArgument

The new activities don’t seem to let you change the TypeArgument of the For Each activity anymore…

which version of UiPath.System.Activities is used?
Which Studio Version / Edition is used
Project Setting (Windows, Legacy)?

Studio - 2022.10.7
Project Setting - Windows

Ok we do know that there are some debatable directions along with bugs / added / removed functionalities. However we can check on which the for each typeargument is interpreted

grafik

And force control e.g. by using the Value / Values / Cast method

myJObject is a JObject
grafik
resulting to:
grafik

the selected method / setting is to derive from the JSON Structure

Awesome, that worked! Thank you!

Would you also happen to know how to get the array items out of “unsubscribedModes”? I can’t seem to get only the string(s) contained within the array.

““phones”” : [ {
““meta”” : {
““lastModified”” : 1670508444104,
““id”” : "“acb821bbeed743859bf4a27c6e96e071"”,
““isActive”” : true,
““isPrimary”” : true,
““timeCreated”” : 1670508444104,
““versionInfo”” : {
““versionNumber”” : 0
}
},
““phoneNumber”” : "“555-114-7712"”,
““dataSource”” : {
““type”” : ““CREDITOR””
},
““unsubscribedModes”” : [ ““SMS”” ]
},

Yes, provoked by the too heavy / not recommended use of toString - returning JSON code

CurrentItem(unsubscribedModes).Values(of String).ToArray - will return a String Array

RnD Series for the proof:
grafik

we can suggest for the learning:

  • exercise separate JSON Data Extraction with simplified structures
  • use the immediate panel and do quick prototypings
1 Like

Hi @ppr - I really appreciate the help, and I will check out that link for the JSON knowledge. As far as this goes, due to the project urgency, I might just have to go with an “if” statement if the JSON contains “unsubscribedModes” and if that contains the string “[ “SMS” ]”. Not ideal, but I can’t get this to work perfectly.

checking if this property exists or not, give a try at

YourJObjectVar.ContainsKey("unsubscribedModes")

1 Like

Oh that works great, thank you for that! This works much better than encountering an error if that key isn’t present.

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