Skip to content

Commit 625c803

Browse files
committed
Added support for parsing an entire json model in a single command
1 parent 3178606 commit 625c803

File tree

6 files changed

+304
-2
lines changed

6 files changed

+304
-2
lines changed

taskt/Core/Automation/Commands/ParseJsonCommand.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class ParseJsonCommand : ScriptCommand
4242
public ParseJsonCommand()
4343
{
4444
this.CommandName = "ParseJsonCommand";
45-
this.SelectionName = "Parse JSON Object";
45+
this.SelectionName = "Parse JSON Item";
4646
this.CommandEnabled = true;
4747
this.CustomRendering = true;
4848

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Xml.Serialization;
5+
using System.Data;
6+
using System.Windows.Forms;
7+
using taskt.UI.Forms;
8+
using taskt.UI.CustomControls;
9+
using System.Drawing;
10+
11+
namespace taskt.Core.Automation.Commands
12+
{
13+
[Serializable]
14+
[Attributes.ClassAttributes.Group("Data Commands")]
15+
[Attributes.ClassAttributes.Description("This command allows you to parse a JSON object into a list.")]
16+
[Attributes.ClassAttributes.UsesDescription("Use this command when you want to extract data from a JSON object")]
17+
[Attributes.ClassAttributes.ImplementationDescription("")]
18+
public class ParseJsonModelCommand : ScriptCommand
19+
{
20+
[XmlAttribute]
21+
[Attributes.PropertyAttributes.PropertyDescription("Supply the value or variable requiring extraction (ex. [vSomeVariable])")]
22+
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
23+
[Attributes.PropertyAttributes.InputSpecification("Select or provide a variable or text value")]
24+
[Attributes.PropertyAttributes.SampleUsage("**Hello** or **vSomeVariable**")]
25+
[Attributes.PropertyAttributes.Remarks("")]
26+
public string v_InputValue { get; set; }
27+
28+
[XmlElement]
29+
[Attributes.PropertyAttributes.PropertyDescription("Assign Objects for Parsing. (ex: $.id)")]
30+
[Attributes.PropertyAttributes.InputSpecification("Inpu")]
31+
[Attributes.PropertyAttributes.SampleUsage("")]
32+
[Attributes.PropertyAttributes.Remarks("")]
33+
public DataTable v_ParseObjects { get; set; }
34+
35+
36+
[XmlIgnore]
37+
[NonSerialized]
38+
private DataGridView ParseObjectsGridViewHelper;
39+
40+
public ParseJsonModelCommand()
41+
{
42+
this.CommandName = "ParseJsonModelCommand";
43+
this.SelectionName = "Parse JSON Model";
44+
this.CommandEnabled = true;
45+
this.CustomRendering = true;
46+
47+
v_ParseObjects = new DataTable();
48+
v_ParseObjects.Columns.Add("Json Selector");
49+
v_ParseObjects.Columns.Add("Output Variable");
50+
v_ParseObjects.TableName = $"ParseJsonObjectsTable{DateTime.Now.ToString("MMddyyhhmmss")}";
51+
52+
ParseObjectsGridViewHelper = new DataGridView();
53+
ParseObjectsGridViewHelper.AllowUserToAddRows = true;
54+
ParseObjectsGridViewHelper.AllowUserToDeleteRows = true;
55+
ParseObjectsGridViewHelper.Size = new Size(400, 250);
56+
ParseObjectsGridViewHelper.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
57+
ParseObjectsGridViewHelper.DataBindings.Add("DataSource", this, "v_ParseObjects", false, DataSourceUpdateMode.OnPropertyChanged);
58+
59+
}
60+
61+
public override void RunCommand(object sender)
62+
{
63+
var engine = (Core.Automation.Engine.AutomationEngineInstance)sender;
64+
65+
//get variablized input
66+
var variableInput = v_InputValue.ConvertToUserVariable(sender);
67+
68+
foreach (DataRow rw in v_ParseObjects.Rows)
69+
{
70+
var jsonSelector = rw.Field<string>("Json Selector").ConvertToUserVariable(sender);
71+
var targetVariableName = rw.Field<string>("Output Variable");
72+
73+
//create objects
74+
Newtonsoft.Json.Linq.JObject o;
75+
IEnumerable<Newtonsoft.Json.Linq.JToken> searchResults;
76+
List<string> resultList = new List<string>();
77+
78+
//parse json
79+
try
80+
{
81+
o = Newtonsoft.Json.Linq.JObject.Parse(variableInput);
82+
}
83+
catch (Exception ex)
84+
{
85+
throw new Exception("Error Occured Parsing Tokens: " + ex.ToString());
86+
}
87+
88+
89+
//select results
90+
try
91+
{
92+
searchResults = o.SelectTokens(jsonSelector);
93+
}
94+
catch (Exception ex)
95+
{
96+
throw new Exception("Error Occured Selecting Tokens: " + ex.ToString());
97+
}
98+
99+
//add results to result list since list<string> is supported
100+
foreach (var result in searchResults)
101+
{
102+
resultList.Add(result.ToString());
103+
}
104+
105+
//get variable
106+
var requiredComplexVariable = engine.VariableList.Where(x => x.VariableName == targetVariableName).FirstOrDefault();
107+
108+
//create if var does not exist
109+
if (requiredComplexVariable == null)
110+
{
111+
engine.VariableList.Add(new Script.ScriptVariable() { VariableName = targetVariableName, CurrentPosition = 0 });
112+
requiredComplexVariable = engine.VariableList.Where(x => x.VariableName == targetVariableName).FirstOrDefault();
113+
}
114+
115+
//assign value to variable
116+
requiredComplexVariable.VariableValue = resultList;
117+
118+
}
119+
120+
121+
122+
123+
124+
}
125+
public override List<Control> Render(frmCommandEditor editor)
126+
{
127+
base.Render(editor);
128+
129+
//create standard group controls
130+
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_InputValue", this, editor));
131+
132+
RenderedControls.Add(CommandControls.CreateDefaultLabelFor("v_ParseObjects", this));
133+
RenderedControls.AddRange(CommandControls.CreateUIHelpersFor("v_ParseObjects", this, new[] { ParseObjectsGridViewHelper }, editor));
134+
135+
RenderedControls.Add(ParseObjectsGridViewHelper);
136+
137+
138+
139+
return RenderedControls;
140+
141+
}
142+
143+
144+
145+
public override string GetDisplayValue()
146+
{
147+
return $"{base.GetDisplayValue()} [Select {v_ParseObjects.Rows.Count} item(s) from JSON]";
148+
}
149+
}
150+
}

taskt/Core/Automation/Commands/ScriptCommand.cs

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ namespace taskt.Core.Automation.Commands
129129
[XmlInclude(typeof(RemoteTaskCommand))]
130130
[XmlInclude(typeof(RemoteAPICommand))]
131131
[XmlInclude(typeof(SeleniumBrowserSwitchFrameCommand))]
132+
[XmlInclude(typeof(ParseJsonModelCommand))]
132133
public abstract class ScriptCommand
133134
{
134135
[XmlAttribute]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Script xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
3+
<Commands>
4+
<ScriptAction>
5+
<ScriptCommand xsi:type="CommentCommand" CommandID="d42a8d88-deea-4c24-a60b-4bab508cf0d5" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="1" PauseBeforeExeucution="false" v_Comment="This sample shows how to parse results from an API Call or JSON Object/Array" CommandEnabled="true" />
6+
</ScriptAction>
7+
<ScriptAction>
8+
<ScriptCommand xsi:type="CommentCommand" CommandID="d4d5350d-bc7d-4d68-a266-934474fce883" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="2" PauseBeforeExeucution="false" v_Comment="Sample 1) Extract and Show User ID From a JSON Object Call" CommandEnabled="true" />
9+
</ScriptAction>
10+
<ScriptAction>
11+
<ScriptCommand xsi:type="CommentCommand" CommandID="ea3ca8c8-e396-40d7-95aa-daccfc472986" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="3" PauseBeforeExeucution="false" v_Comment="The following API will return a single item" CommandEnabled="true" />
12+
</ScriptAction>
13+
<ScriptAction>
14+
<ScriptCommand xsi:type="RESTCommand" CommandID="21271001-50de-4b46-aa85-3ab6d1238b9c" CommandName="RESTCommand" IsCommented="false" SelectionName="Execute REST API" DefaultPause="0" LineNumber="4" PauseBeforeExeucution="false" CommandEnabled="true" v_BaseURL="https://jsonplaceholder.typicode.com" v_APIEndPoint="/todos/1" v_APIMethodType="GET" v_userVariableName="vAPIResult" v_RequestFormat="Json">
15+
<v_AdvancedParameters>
16+
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
17+
<xs:element name="A11vRESTPara9Table121119.100948">
18+
<xs:complexType>
19+
<xs:sequence>
20+
<xs:element name="Parameter_x0020_Name" type="xs:string" minOccurs="0" />
21+
<xs:element name="Parameter_x0020_Value" type="xs:string" minOccurs="0" />
22+
<xs:element name="Content_x0020_Type" type="xs:string" minOccurs="0" />
23+
<xs:element name="Parameter_x0020_Type" type="xs:string" minOccurs="0" />
24+
</xs:sequence>
25+
</xs:complexType>
26+
</xs:element>
27+
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="A11vRESTPara9Table121119.100948" msdata:UseCurrentLocale="true">
28+
<xs:complexType>
29+
<xs:choice minOccurs="0" maxOccurs="unbounded">
30+
<xs:element ref="A11vRESTPara9Table121119.100948" />
31+
</xs:choice>
32+
</xs:complexType>
33+
</xs:element>
34+
</xs:schema>
35+
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" />
36+
</v_AdvancedParameters>
37+
<v_RESTParameters>
38+
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
39+
<xs:element name="RESTPara51Table041619.065138">
40+
<xs:complexType>
41+
<xs:sequence>
42+
<xs:element name="Parameter_x0020_Type" type="xs:string" minOccurs="0" />
43+
<xs:element name="Parameter_x0020_Name" type="xs:string" minOccurs="0" />
44+
<xs:element name="Parameter_x0020_Value" type="xs:string" minOccurs="0" />
45+
</xs:sequence>
46+
</xs:complexType>
47+
</xs:element>
48+
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="RESTPara51Table041619.065138" msdata:UseCurrentLocale="true">
49+
<xs:complexType>
50+
<xs:choice minOccurs="0" maxOccurs="unbounded">
51+
<xs:element ref="RESTPara51Table041619.065138" />
52+
</xs:choice>
53+
</xs:complexType>
54+
</xs:element>
55+
</xs:schema>
56+
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" />
57+
</v_RESTParameters>
58+
</ScriptCommand>
59+
</ScriptAction>
60+
<ScriptAction>
61+
<ScriptCommand xsi:type="CommentCommand" CommandID="8aa6e23b-1fb7-4d51-886f-6c8185e7a26b" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="5" PauseBeforeExeucution="false" v_Comment="Show API Result" CommandEnabled="true" />
62+
</ScriptAction>
63+
<ScriptAction>
64+
<ScriptCommand xsi:type="MessageBoxCommand" CommandID="77a340fd-b895-488b-a640-de2e78ccf669" CommandName="MessageBoxCommand" IsCommented="false" SelectionName="Show Message" DefaultPause="0" LineNumber="6" PauseBeforeExeucution="false" CommandEnabled="true" v_Message="THE FOLLOWING IS THE RESULT FROM THE API: {vAPIResult}" v_AutoCloseAfter="0" />
65+
</ScriptAction>
66+
<ScriptAction>
67+
<ScriptCommand xsi:type="CommentCommand" CommandID="51cc68aa-9160-497a-9c86-fad5f86178cb" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="7" PauseBeforeExeucution="false" v_Comment="Parse Individual Properties from API Result" CommandEnabled="true" />
68+
</ScriptAction>
69+
<ScriptAction>
70+
<ScriptCommand xsi:type="ParseJsonCommand" CommandID="63136e34-3fbb-41db-b619-77ed244bc5be" CommandName="ParseJsonCommand" IsCommented="false" SelectionName="Parse JSON" DefaultPause="0" LineNumber="8" PauseBeforeExeucution="false" CommandEnabled="true" v_InputValue="{vAPIResult}" v_JsonExtractor="$.userId" v_applyToVariableName="vUserID" />
71+
</ScriptAction>
72+
<ScriptAction>
73+
<ScriptCommand xsi:type="ParseJsonCommand" CommandID="f8ef07fd-7fff-4e37-b075-7f9eb5c7114e" CommandName="ParseJsonCommand" IsCommented="false" SelectionName="Parse JSON" DefaultPause="0" LineNumber="9" PauseBeforeExeucution="false" CommandEnabled="true" v_InputValue="{vAPIResult}" v_JsonExtractor="$.title" v_applyToVariableName="vTitle" />
74+
</ScriptAction>
75+
<ScriptAction>
76+
<ScriptCommand xsi:type="CommentCommand" CommandID="5819c5a0-5b2b-425a-a1df-21ece7106d83" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="10" PauseBeforeExeucution="false" v_Comment="Show Parsed Variables" CommandEnabled="true" />
77+
</ScriptAction>
78+
<ScriptAction>
79+
<ScriptCommand xsi:type="MessageBoxCommand" CommandID="c2704947-8bc1-4ac5-86cc-f743a8a33f9d" CommandName="MessageBoxCommand" IsCommented="false" SelectionName="Show Message" DefaultPause="0" LineNumber="11" PauseBeforeExeucution="false" CommandEnabled="true" v_Message="SINGLE EXTRACTION RESULT: ID '{vUserID}' WITH TITLE '{vTitle}'" v_AutoCloseAfter="0" />
80+
</ScriptAction>
81+
<ScriptAction>
82+
<ScriptCommand xsi:type="CommentCommand" CommandID="7b33b6ac-3fca-4b66-9570-159a0a68592f" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="12" PauseBeforeExeucution="false" v_Comment="Parse multiple properties in a single command" CommandEnabled="true" />
83+
</ScriptAction>
84+
<ScriptAction>
85+
<ScriptCommand xsi:type="ParseJsonModelCommand" CommandID="4da9903f-200b-44f5-96c4-befdf1681f14" CommandName="ParseJsonModelCommand" IsCommented="false" SelectionName="Parse JSON Model" DefaultPause="0" LineNumber="13" PauseBeforeExeucution="false" CommandEnabled="true" v_InputValue="{vAPIResult}">
86+
<v_ParseObjects>
87+
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
88+
<xs:element name="ParseJsonObjectsTable121119104203">
89+
<xs:complexType>
90+
<xs:sequence>
91+
<xs:element name="Json_x0020_Selector" type="xs:string" minOccurs="0" />
92+
<xs:element name="Output_x0020_Variable" type="xs:string" minOccurs="0" />
93+
</xs:sequence>
94+
</xs:complexType>
95+
</xs:element>
96+
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="ParseJsonObjectsTable121119104203" msdata:UseCurrentLocale="true">
97+
<xs:complexType>
98+
<xs:choice minOccurs="0" maxOccurs="unbounded">
99+
<xs:element ref="ParseJsonObjectsTable121119104203" />
100+
</xs:choice>
101+
</xs:complexType>
102+
</xs:element>
103+
</xs:schema>
104+
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
105+
<DocumentElement>
106+
<ParseJsonObjectsTable121119104203 diffgr:id="ParseJsonObjectsTable1211191042031" msdata:rowOrder="0" diffgr:hasChanges="inserted">
107+
<Json_x0020_Selector>$.id</Json_x0020_Selector>
108+
<Output_x0020_Variable>vModelID</Output_x0020_Variable>
109+
</ParseJsonObjectsTable121119104203>
110+
<ParseJsonObjectsTable121119104203 diffgr:id="ParseJsonObjectsTable1211191042032" msdata:rowOrder="1" diffgr:hasChanges="inserted">
111+
<Json_x0020_Selector>$.title</Json_x0020_Selector>
112+
<Output_x0020_Variable>vModelTitle</Output_x0020_Variable>
113+
</ParseJsonObjectsTable121119104203>
114+
</DocumentElement>
115+
</diffgr:diffgram>
116+
</v_ParseObjects>
117+
</ScriptCommand>
118+
</ScriptAction>
119+
<ScriptAction>
120+
<ScriptCommand xsi:type="MessageBoxCommand" CommandID="88670db2-8d81-470d-8d3a-bd41056de353" CommandName="MessageBoxCommand" IsCommented="false" SelectionName="Show Message" DefaultPause="0" LineNumber="14" PauseBeforeExeucution="false" CommandEnabled="true" v_Message="MULTIPLE EXTRACTION FROM MODEL WAS '{vModelID}' WITH TITLE '{vModelTitle}'" v_AutoCloseAfter="0" />
121+
</ScriptAction>
122+
</Commands>
123+
<Variables>
124+
<ScriptVariable>
125+
<VariableName>vAPIResult</VariableName>
126+
<VariableValue xsi:type="xsd:string"></VariableValue>
127+
</ScriptVariable>
128+
<ScriptVariable>
129+
<VariableName>vBody</VariableName>
130+
<VariableValue xsi:type="xsd:string"></VariableValue>
131+
</ScriptVariable>
132+
<ScriptVariable>
133+
<VariableName>vComments</VariableName>
134+
<VariableValue xsi:type="xsd:string"></VariableValue>
135+
</ScriptVariable>
136+
<ScriptVariable>
137+
<VariableName>vEmail</VariableName>
138+
<VariableValue xsi:type="xsd:string"></VariableValue>
139+
</ScriptVariable>
140+
<ScriptVariable>
141+
<VariableName>vTitle</VariableName>
142+
<VariableValue xsi:type="xsd:string"></VariableValue>
143+
</ScriptVariable>
144+
<ScriptVariable>
145+
<VariableName>vUserID</VariableName>
146+
<VariableValue xsi:type="xsd:string"></VariableValue>
147+
</ScriptVariable>
148+
</Variables>
149+
</Script>

taskt/UI/CustomControls/CustomControls.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,8 @@ public static Dictionary<string, Image> UIImageDictionary()
526526
uiImages.Add("LogDataCommand", taskt.Properties.Resources.command_files);
527527
uiImages.Add("ExecuteDLLCommand", taskt.Properties.Resources.command_run_code);
528528
uiImages.Add("RESTCommand", taskt.Properties.Resources.command_run_code);
529-
uiImages.Add("ParseJsonCommand", taskt.Properties.Resources.command_parse);
529+
uiImages.Add("ParseJsonCommand", taskt.Properties.Resources.command_parse);
530+
uiImages.Add("ParseJsonModelCommand", taskt.Properties.Resources.command_parse);
530531
uiImages.Add("ParseJsonArrayCommand", taskt.Properties.Resources.command_parse);
531532
uiImages.Add("UploadDataCommand", taskt.Properties.Resources.command_server);
532533
uiImages.Add("GetDataCommand", taskt.Properties.Resources.command_server);

taskt/taskt.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@
172172
<Compile Include="Core\Automation\Commands\CatchExceptionCommand.cs" />
173173
<Compile Include="Core\Automation\Commands\EndTryCommand.cs" />
174174
<Compile Include="Core\Automation\Commands\FinallyCommand.cs" />
175+
<Compile Include="Core\Automation\Commands\ParseJsonModelCommand.cs" />
175176
<Compile Include="Core\Automation\Commands\RemoteAPICommand.cs" />
176177
<Compile Include="Core\Automation\Commands\RemoteTaskCommand.cs" />
177178
<Compile Include="Core\Automation\Commands\SeleniumBrowserSwitchFrameCommand.cs" />

0 commit comments

Comments
 (0)