I have a text variable with a sample value, and I want to deserialize it into JSON format so I can store it in my existing list structure (ignore the extra values).
However, when I attempt to use JSON deserialization, it returns an empty value. I suspect this might be due to a data type issue, but I’d prefer not to create a separate structure (sample below), just to extract key objects, especially since the list contains a large number of countries. I'm unsure how to properly deserialize the string—any suggestions?
Hi @Cherrylyn Cardiel, from what I can see the problem is dynamic key, notice that in the structure created by OutSystems the attribute right after data is labeled as text because, it does not have a fix label as key, hence OutSystems can not find anything inside data. Here is what I can suggest, you need to manipulate the JSON in an acceptable format by OutSystems, for this what you will need to do is create an extension function which converts this JSON in desired format. Then create an OnAfterResponse for your API and call this new extension function inside the OnAfterResponse and reassign the value of 'ResponseText' vairable with the Output of this new function and lastly you need to use the new JSON structure as your API output structure (refer the output I am adding below).
I am adding the .Net code below, but I have not tested this code in .NET, I have created this code in JS and tested it then converted it to .NET using AI, for reference I am also attaching JS code as well so that you can test it.
.NET code:
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class JsonTransformer
{
public string TransformJson(string inputJsonString)
// Parse the JSON string into a JObject
var originalJson = JObject.Parse(inputJsonString);
// Create a new structure for the converted JSON
var convertedJson = new JObject
["Data"] = new JObject
["Locations"] = new JArray() // Use an array to hold multiple locations
}
};
// Iterate over the original data and add each location to the array
foreach (var location in originalJson["Data"])
convertedJson["Data"]["Locations"].Last.Add(location.First);
// Return the converted JSON as a string
return convertedJson.ToString(Formatting.Indented);
JS Code:
// JSON string input
const jsonString = $parameters.Input;
// Parse the JSON string into an object
const originalJson = JSON.parse(jsonString);
// Convert to desired format
const convertedJson = {
"Data": {
"Locations": [] // Use an array to hold multiple locations
// Iterate over the original data and push each location into the array
for (const key in originalJson.Data) {
if (originalJson.Data.hasOwnProperty(key)) {
convertedJson.Data.Locations.push(originalJson.Data[key]);
// Output the converted JSON
$parameters.Output = JSON.stringify(convertedJson, null, 4);
Input:
"PH": {
"Id": "PH",
"Name": "Philippines",
"Address": {
"Country": "PH",
"state": null,
"Region": null,
"Province": "Lampang",
"City": null
},
"Description": null,
"Coordinates": {
"Longitude": 99.4727335,
"Latitude": 18.2796412
"Images": []
"TH": {
"Id": "TH",
"Name": "Thailand",
"Country": "TH",
"State": null,
"Province": "null",
Output:
"Locations": [
]
ThanksGitansh Anand
Right. I was wondering how to remove the keys so I could format it in the acceptable JSON format in outsystem. I tried your applying your solution in integration studio and it worked. Thanks.
@Cherrylyn Cardiel
The Json string which you have shared is not a proper there are some mistakes please correct from below json format.
"PH": {"Id": "PH","Name": "Philippines",
"Address": {"Country": "PH","state": null,"Region": null,"Province": "Lampang","City": null},
"Coordinates": {"Longitude": 99.4727335,"Latitude": 18.2796412},"Images": ""},
"TH": {"Id": "TH","Name": "Thailand",
"Address": {"Country": "TH","State": null,"Region": null,"Province": "null","City": null},
"Coordinates": {"Longitude": 99.4727335,"Latitude": 18.2796412},"Images": ""}
And when creating a structure in Outsystems you can use a "Add structure from JSON" which creates automatically.
Thanks but I am aware of the automatic creation of structure from JSON. My concern is it'll create a structure like the last screenshot I have posted which is not efficient for long list of countries so I wanted convert it to a new structure like in the first screenshot.
Your JSON format is incorrect, please check that.
Hello,
Once you have your JSON corrected as pointed by Nani you could create a structure through built in feature "Add Structure From JSON". For better retrieval of elements from this structure you could use the forge component https://www.outsystems.com/forge/component-overview/413/ardojson-o11
Hope it helps!
Junaid
Hi ,
You can delete the TH item from your structure and create a list of PH type and then try deserializing as you said there can be a number of countries.
hope it helps,
Thanks
can you share oml and json string not image for check ?
you can manually change or create a new structure according to your json some time automatic generation create problem so you can fix it manually.
You can fix that string with RegEx.
First remove the county code:
Regex_Replace(JSON,"""([A-Z]{2})"":\s*{", "{", True,True,False)
Then adapt the extremes of the string to become a list.
Regex_Replace(NewString,"{\s*""Data"":\s*{","[",True,True,False)
Regex_Replace(NewString,"}\s*}$","]",True,True,False)
It will look like this, a valid JSON list.
[ {"Id": "PH", "Name": "Philippines",
"Address": {"Country": "PH", "state": None, "Region": None, "Province": "Lampang", "City": None},
"Description": None,
"Coordinates": {"Longitude": 99.4727335, "Latitude": 18.2796412}, "Images": ""},
{"Id": "TH", "Name": "Thailand",
"Address": {"Country": "TH", "State": None, "Region": None, "Province": "null", "City": None}, "Description": None,
"Coordinates": {"Longitude": 99.4727335, "Latitude": 18.2796412}, "Images": ""}
Thanks for this. I think this is the shortest way and will work as well.