Help to parse a Json I get from a webservice to variables can after introduce to a Creatio Object

Hello I'm getting in a process the following string from a webservice and I want to parse it to and object where can I get the elements the json bring to me, for example I get:

{"version":"1.6.0","autor":"mindicador.cl","codigo":"uf","nombre":"Unidad de fomento (UF)","unidad_medida":"Pesos","serie":[{"fecha":"2020-04-09T04:00:00.000Z","valor":28630.63}]}

 

And I need to parse to get:

nombre

fecha

valor

 

How can I did it?

 

Thanks

Like 0

Like

6 comments

Hello, Julio!

 

Please see the article below on working with the web service response:

https://academy.creatio.com/documents/technic-bpms/7-15/call-web-servic…

If you want to do this differently, you can call web service with a standard C# tools using a script task element and process the response body in a script task. Also, for more flexibility, you can call the web serviced is a script task. Here is an article with an example: 

https://stackoverflow.com/questions/3900371/how-can-i-call-a-webservice…

To parse JSON in script task you can use the following article, for example: 

https://www.codementor.io/@andrewbuchan/how-to-parse-json-into-a-c-object-4ui1o0bx8

Dennis Hudson,

Hello Dennis,

 

I don't know if you are still supporting the community forum, however if you are.  The JSON article uses the System.Web.Script namespace.  When I compile a script task using this namespace an error results saying Script not found within System.Web.  Is there are workaround or an alternative?  Thanks,

Gareth Osler,

 

Hello,

 

As an example I've used the string Julio shared above. To deserialize it I've used the following approach:

 

1) Create a source code in configurations. Name it UsrForCustomObject. The content is as follows:

using System.Collections.Generic;
using Terrasoft.Configuration;
 
namespace Terrasoft.Configuration.UsrForCustomObject
{
	public class UsrForCustomObject
    {
        public string version
        { get; set; }
        public string autor
        { get; set; }
        public string codigo
        { get; set; }
        public string nombre
        { get; set; }
        public string unidad_medida 
        { get; set; }
        public List<InternalUsrForCustomObjectList> serie
        { get; set; }
    }
 
    public class InternalUsrForCustomObjectList
    {
        public string fecha 
        { get; set; }
        public decimal valor 
        { get; set; }
    }
}

2) Create a business process with the following schema:

The code of the parameters is the same as the name:

2.1) Text (500 characters) parameter - ParsedFecha

2.2) Text (500 characters) parameter - ParsedNombre

2.3) Decimal (0.01) parameter - ParsedValor

2.4) Text (500 characters) parameter - ParsedVersion

2.5) Text (500 characters) parameter - PassedJSONString

 

This is the content of the "Methods" tab:

public void PerformJsonDeserialize()
{
	var passedValue = Get<string>("PassedJSONString");
	if (!String.IsNullOrEmpty(passedValue))
	{
		UsrForCustomObject testCustObj = System.Text.Json.JsonSerializer.Deserialize<UsrForCustomObject>(passedValue);
		Set<string>("ParsedVersion", testCustObj.version);
		Set<string>("ParsedNombre", testCustObj.nombre);
		Set<string>("ParsedFecha", testCustObj.serie[0].fecha);
		Set<decimal>("ParsedValor", testCustObj.serie[0].valor);
	}
}

and we are using Terrasoft.Configuration.UsrForCustomObject.

 

Why it's done so: we need to deserialize the passed string and receive an instance of the UsrForCustomObject class we've declared at step 1 in configurations. Deserialization is performed using System.Text.Json.JsonSerializer.Deserialize method (please note that specifying JsonSerializer.Deserialize method with specifying System.Text.Json as an assembly reference will result in error stating that the system cannot understand whether System.Text.Json.JsonSerializer.Deserialize should be used or Newtonsoft.Json.JsonSerializer.Deserialize should be used, so use the exact approach of method call as in my example above).

 

3) "Input JSON string" auto-generated page is quite simple: it tells the user to input JSON string. It's done for testing purposes, in fact actual JSON string can be passed:

4) In the "Set JSON to parameters" formula we specify the value for the "PassedJSONString" parameter that should be the string we've input in the "Input JSON string" auto-generated page from step 3:

5) In the "Deserialize" script-task we call the "PerformJsonDeserialize" method from the process methods from step 2:

6) In the "Result" auto-generated page we simply show the result of deserialization (values for all parameters from step 2):

 

So as a result here is the screenshot of the process start "Input JSON string" auto-generated page where the value of:

{"version":"1.6.0","autor":"mindicador.cl","codigo":"uf","nombre":"Unidad de fomento (UF)","unidad_medida":"Pesos","serie":[{"fecha":"2020-04-09T04:00:00.000Z","valor":28630.63}]}

is passed:

and here is the screenshot of the "Result" auto-generated page:

Also these parsed values can be converted (to date\time for example) and so on. So please feel free to study this example and implement the same logic on your end.

A quick note for anyone who happens this way in the future.

Step 1) above, the namespace name needs to be different from the class name, e.g.:

using System.Collections.Generic;
using Terrasoft.Configuration;
 
namespace Terrasoft.Configuration.UsrCustomerImportNamespace
{
	public class UsrCustomerImport
    {
        public string accountName { get; set; }
        public List<UsrCustomerImportItems> items { get; set; }
    }
 
    public class UsrCustomerImportItems
    {
        public string contactName { get; set; }
        public string contactType { get; set; }
    }
}

 

By way of an epilogue, while the `System.Text.Json` namespace is available in a trial, for some reason it was not available in the dev environment I was using.  However classes in the `Terrasoft.Common.Json` namespace were available.  I was able to use the `Deserialize<T>(String)` method of the Json class.  (Note at the time of writing the `DeserializeDictionary(String)` method has a bug in it and cannot be used.)

@Oleg, 

 

I followed the steps above but receive the attached upon compilation of the methods within business process? 

Show all comments