Converting Text value to JSON

Hi,


I want to convert a text value into JSON. Please find the below text value 

"data":"{

""state"" :""A"",

""Country"":""B""

}"

I want the data in below JSON format

"data": {

"state": "A",

"country": "B"

}


Thanks & Regards,

Sowndharya

Hi Sowndharya,

If you have a text containing a JSON string then you can use the DezerializeJSON client or server action to convert it into an OutSystems structure.

The structure you can schafold on the Data tab using your example JSON string.

My colleague Joao wrote a nice article about it https://itnext.io/serializing-deserializing-json-in-the-outsystems-platform-ae08f2f6b5b4

Regards,

Daniel 

Hi Daniel,


Thanks for your reply.

The number of fields will vary in JSON. This is not fixed. 

"data":"{

""state"" :""A"",

""Country"":""B""

}"


Right now, there are 2 fields in JSON. In the future, the number of fields will be increased. So, I can't use structure over here.


Thanks & Regards,

Sowndharya

Hello Sowndharya,

I have an idea for you,

You first validate whether your Text is correct JSON format.

If it is wrong then you have to correct the text format to valid JSON.

you can try,

https://www.outsystems.com/forge/component-overview/1948/json-validator


To process a JSON in OutSystems you need to deserialize it to a structure. Structure should contain all possible fields. If the JSON contains less it will just not fill those structure attributes.

You can not process a JSON string randomly into some kind of dynamically created structure.

What would you do with future added elements in your JSON without changing your logic?