Extract specific token from a complicated Json string returned from API call

Hi,

Using a HTTP request to GET data from an app via its API, I get the Json string below:
“{"Id":"11e62b7a-25c8-4190-bb12-c5f8bd87d0df","Status":"OK","ProviderName":"Health Accelerator App","DateTimeUTC":"/Date(1749441203880)/","Contacts":[{"ContactID":"db3cf355-60eb-426b-bfe1-b6f1081ac0f9","AccountNumber":"indici0001","ContactStatus":"ACTIVE","Name":"Acme Health","FirstName":"Phillip","LastName":"Duncan","EmailAddress":"phillip.duncan@acme.co.nz","Addresses":[{"AddressType":"POBOX","AddressLine1":"16 XXX Lane","AddressLine2":"Parnell","City":"Auckland","PostalCode":"1010","Country":"New Zealand"},{"AddressType":"STREET","AddressLine1":"16 XXX Lane","AddressLine2":"Parnell","City":"Auckland","PostalCode":"1010","Country":"New Zealand"}],"Phones":[{"PhoneType":"DDI"},{"PhoneType":"DEFAULT"},{"PhoneType":"FAX"},{"PhoneType":"MOBILE"}],"UpdatedDateUTC":"/Date(1749086743507+0000)/","ContactGroups":,"IsSupplier":false,"IsCustomer":true,"Balances":{"AccountsReceivable":{"Outstanding":130.98,"Overdue":130.98},"AccountsPayable":{"Outstanding":0.0,"Overdue":0.0}},"ContactPersons":,"HasAttachments":false,"HasValidationErrors":false}]}”

(This is test data, by the way - no confidentiality / privacy issue here…)

What I am after extracting is the ContactID value.

I have tried to deserialize the string into a JObject, but I am unsure from get to ContactID token’s value there how to, especially as it is inside an array in the string.

Any help will be much appreciated!

Hi @Phillip_Duncan

=> The output of HTTP request say jsonString.
=> Use Deserialize JSON activity to Deserialize the json which you get and store it in a variable say jObj.
=> Use below syntax in Assign activity:

contactsArray = CType(jObj("Contacts"), Newtonsoft.Json.Linq.JArray)

Note: contactsArray is of DataType Newtonsoft.Json.Linq.JArray
=> Use below syntax in Assign activity:

contactID = contactsArray(0)("ContactID").ToString()

=> Use contactID whereever required or you can print it in Log Message or Message Box.

Check the below workflow for better understanding:
Sequence.xaml (8.3 KB)

Hope it helps!!

1 Like

Hi @Phillip_Duncan

Use Deserialize JSON activity on the string and output variable as jsonObject

Directly use Assign Activity

contactID = jsonObj("Contacts")(0)("ContactID").ToString

Hope this helps!
image

1 Like

Hi @Phillip_Duncan

If you’re using the newer System.Text.Json:
using Newtonsoft.Json.Linq;

// Parse the JSON string into a JObject
JObject jsonObject = JObject.Parse(jsonString);

// Navigate to the Contacts array, get the first item, and extract the ContactID
string contactID = jsonObject[“Contacts”][0][“ContactID”].ToString();

// Output: “db3cf355-60eb-426b-bfe1-b6f1081ac0f9”
Console.WriteLine(contactID);

Handling Multiple Contacts

If you need to extract ContactID from all contacts in the array:

// Using Newtonsoft.Json
JArray contactsArray = (JArray)jsonObject[“Contacts”];
foreach (var contact in contactsArray)
{
string id = contact[“ContactID”].ToString();
Console.WriteLine(id);
}

The key is understanding that Contacts is an array, so you need to access the appropriate index (in this case [0] ) before you can access the ContactID property

1 Like

Thank you all for your help.

In the end the solution by @Sanjay_Bhat was the one I went with.

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