From the Purchase Orders form I'm trying to call an API Gateway I've created, get results, and save to the SLPos IDO.
hi Richard, we have a whole extensibility playlist on YouTube or you should be able to find some more information on how to do this. Below is a video on how to create an IDO based off of an API in the gateway. Hopefully this helps.
https://youtu.be/ShaKj5VA1Ug?si=LZsQsuzItIY59jmK
Hi Caitlin,
Thanks for the pointers.
Do you have something that will show how to parse Json in the Form Script (guess that would be a Mongoose object I think)?
The video shows how to get a response and display it in a multi-edit textbox.
I need to parse the response and extract specific information from it.
Thanks
//<ref>Newtonsoft.Json.dll</ref> using System; using Mongoose.IDO.Protocol; using Mongoose.Scripting; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace Mongoose.GlobalScripts { public class ParseJson : GlobalScript { //Param 1: JSON String //Param 2: Target to search for public void Main() { string jsonStr = ""; string target = ""; string ret1 = ""; try { jsonStr = GetParameter(0); target = GetParameter(1); ret1 = GetParameter(2); var jsonData = JObject.Parse(jsonStr); //Application.ShowMessage(jsonStr); //ReturnValue = (string) ExtractValue(jsonData, target); ThisForm.Variables(ret1).Value = (string) ExtractValue(jsonData, target); //Application.ShowMessage(ThisForm.Variables(ret1).Value); } catch (Exception e) { ThisForm.Variables("varDebug").Value += " could not parse json\n\n" + jsonStr; //Application.ShowMessage(ThisForm.Variables("varDebug").Value); ReturnValue = "0"; } } private JToken ExtractValue(JToken json, string key) { if (json is JObject obj) { //case-insensitive var normalizedKey = key.ToLower(); foreach (var property in obj.Properties()) { if (property.Name.ToLower() == normalizedKey) return property.Value; var result = ExtractValue(property.Value, key); if (result != null) return result; } } else if (json is JArray array) { foreach (var item in array) { var result = ExtractValue(item, key); if (result != null) return result; } } return null; } } }
Thanks! I apologize, should have mentioned that we're in the cloud and our form script is vb.
' <ref>Newtonsoft.Json.dll</ref>
Imports System Imports Mongoose.IDO.Protocol Imports Mongoose.Scripting Imports Newtonsoft.Json Imports Newtonsoft.Json.Linq
Namespace Mongoose.GlobalScripts
Public Class ParseJson Inherits GlobalScript ' Param 0: JSON string ' Param 1: Target key to search for ' Param 2: Name of form variable to receive the value Public Sub Main() Dim jsonStr As String = "" Dim target As String = "" Dim ret1 As String = "" Try jsonStr = GetParameter(0) target = GetParameter(1) ret1 = GetParameter(2) Dim jsonData As JObject = JObject.Parse(jsonStr) Dim extracted As JToken = ExtractValue(jsonData, target) If extracted IsNot Nothing Then ThisForm.Variables(ret1).Value = CStr(extracted) Else ThisForm.Variables(ret1).Value = "" End If Catch ex As Exception ThisForm.Variables("varDebug").Value &= " could not parse json" & vbCrLf & vbCrLf & jsonStr ReturnValue = "0" End Try End Sub Private Function ExtractValue(json As JToken, key As String) As JToken If TypeOf json Is JObject Then Dim obj As JObject = DirectCast(json, JObject) ' case insensitive match Dim normalizedKey As String = key.ToLower() For Each prop As JProperty In obj.Properties() If prop.Name.ToLower() = normalizedKey Then Return prop.Value End If Dim result As JToken = ExtractValue(prop.Value, key) If result IsNot Nothing Then Return result End If Next ElseIf TypeOf json Is JArray Then Dim arr As JArray = DirectCast(json, JArray) For Each item As JToken In arr Dim result As JToken = ExtractValue(item, key) If result IsNot Nothing Then Return result End If Next End If Return Nothing End Function End Class
End Namespace