218
Views
16
Comments
Object to text
Question

Hi,

To convert text to object in O11 Toobject() Function is available, But how to convert Object to text

 Can anyone suggest any way to convert that?



Thanks

2019-01-31 08-54-57
André Costa
Champion

Hi @Manikandan M !

While there isn't a direct built-in function like ToObject(), you can achieve this by using the JSON.Serialize() function. This function converts the object into a JSON string.


You can place this logic inside an action where you handle the conversion. After serializing the object, you can use the resulting text as needed.


UserImage.jpg
SASIKUMAR S

Hi André Costa,
 Thank you for your reply,
Can you please help me where I can find the JSON.Serialize() function, Is that Json Serialise Widget or any Function available in the system as well,
While trying with the Widget, I cannot pass an Object as input for the Json Serialise widget.

2021-11-19 11-12-44
Rui Mendes


The widgets for converting data to and from JSON format are available in the Tools section on the left side when you are in the Logic tab, specifically under Actions.

If this explanation helps you achieve what you need, it would be beneficial to mark it as a solution for others who might encounter the same issue in the future.

If you provide an example of the text you want to work with, I might be able to help you better, as you will need a structure for that data.

UserImage.jpg
SASIKUMAR S

Hi Rui Mendes,

Thank you for your reply,

Am getting an Object from API, and that is not Binary, Is there any way to convert the object to Binary so that it can be converted to Text which will be easy to Deserialize the Object?

2021-11-19 11-12-44
Rui Mendes


Hello @Manikandan M

In this case, it doesn't make sense to convert the text back to binary. What you want is to handle the response from an API, so this text should already be formatted in a common language such as XML, JSON, or another markup language.  Look for the API documentation or an example of the response to guide you towards the correct solution.  If you already have the API integrated within OutSystems, then you have an easy way to find out. I will show you how in the following image. 

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Not sure why you reply this, but JSONSerialize cannot be used with an Object-type variable. It's easy to test:

Please do not spread false information!

2024-12-18 16-06-42
Junaid Syed

Hello Manikandan,

The ArdoJSON forge component's OutSystems2JSON action might be useful in your situation.

Hope it helps!

Junaid

UserImage.jpg
SASIKUMAR S

Hello Junaid Syed,
Thanks for replying,

I have tried that Extension but am getting as empty JSON as Output. Please, find the Screenshot.

If I did anything wrong in that please help me with that.

2021-11-19 11-12-44
Rui Mendes


This extension was meant to be used in earlier versions of OutSystems; nowadays, we have the method I showed you, which replaces this one. 

As for receiving an empty string, there can be several reasons for that. Initially, I would say that the data might not be in JSON format. 

If you could provide an example of the data, it would be easier for me to guide you in the right direction towards a solution.

2024-12-18 16-06-42
Junaid Syed

Please share the OML file to better understand your situation and suggest any solution.

UserImage.jpg
SASIKUMAR S

Hi Junaid,
  I am unable to share the OML, I have replied below the scenario for converting JSON object to JSON Array, Could you pls suggest any way to achieve that?
Thank you

2019-01-07 16-04-16
Siya
 
MVP

@Manikandan M : Since you mentioned you are receiving Object from API,  can you enable Full Logging of your service and share the log ( after obfuscating the sensitive data if any ) ? This might help to suggest a better solution.

UserImage.jpg
SASIKUMAR S

Hi Siya,

   Thanks for replying ,I am receiving both Json object and json array from API, I need to convert those JSON object to Json array, Like Mentioned below

INPUT:

{

   "jsonObject":

{

      "name":"John",

      "age":30,

      "city":"New York"

 }

}

OUTPUT:

{

   "jsonObject":

[ {

         "name":"John",

         "age":30,

         "city":"New York"   } ]

}

How to achieve this?

2021-11-19 11-12-44
Rui Mendes

you can see the attached example how to process this response.

Exemple.oml
2019-01-07 16-04-16
Siya
 
MVP

@Manikandan M , As I understand from your previous response, you want to convert Input JSON (jsonObject)  to Output JSON  (jsonObject Array).  I have created structure using the JSON provided as below 

Then I deserialised the input JSON. to an variable of type JsonObject and then added that item to a new variable of type JsonObjectArray

and finally I have something like this

btw is this what you are trying to achieve something else ?

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Manikandan,

You are confusing the OutSystems Object data type with a JSON object. Those two are very different. The OutSystems Object can and should only be used in conjunction with Extensions. They encapsulate data (typically a state) from an Extension that can dan be passed back to the Extension, or to pass a Record or Record List to an Extension as a generic data type, in which case you'd use ToObject(). You can do nothing at all with an Object in OutSystems outside an Extension.

JSON objects on the other hand are represented in OutSystems by Structures. To convert JSON to a Structure (or a List of Structures) you'd use JSONDeserialize. To convert a Structure (or List of Structures) to JSON, you'd use JSONSerialize. However, when consuming or exposing an API, this conversion is done automatically for you by the Platform. Only in edge-cases would you (de)serialize manually in the code.

Above you mentioned that you receive a single JSON object, but need a JSON list. Can you explain why you need this? What is your use case? Because typically, using OutSystems, you would convert the JSON object to a variable of a Structure type, and then you can add this to a List if you want (using ListAppend). Or do you need to convert it before deserializing, because the API has polymorphic output, sometimes returning a single object, sometimes an array of objects?

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.