ZennoLab

Automate everything

User Tools

Site Tools


Sidebar

Translations of this page:

en:json_xml

Processing JSON/XML

Web-services sometimes return info in JSON or XML format and it should be processed in special way to get data.

For this we added “Processing JSON/XML” action.

Parsing

The first way of work is parsing JSON/XML string.

Data can be parsed using JSONPath or XPath. Parsing result will be put to project variable Json or XML, which you can see in Variables window:

To access variables the following macros can be used {-Json.employees.Count-} or {-Json.employees[2].firstName-}.

If parsing was successful, hints will be abailable in every input field where macros are supported.

Saving to list or table

There may be arrays of data in parsed JSON/XML and it's convenient to save result to list or table.

The “Property” specifies the field which will be processed as array. There is also possible to specify subfield using dot delimiter, for example “store.employees”. If the field is not an array, only one value will be saved to list. The “Subproperty” allows to select certain value from the field for the list, if array contains complex objects.

Saving to table is almost the same. “Subproperty” values can be put to certain column in a table.

\

Processing XML

There may be node attributes in XML data. To access such attributes from macro square brackets can be use with string value in them, for example: {-Xml.PurchaseOrder[«PurchaseOrderNumber»]-}.

Processing JSON/XML from code

JSON and XML objects can be access from project object in C#. They has dynamic type and code editor does not allow to display hints for them.

Examples processing XML data:

Example 1:

project.Xml.FromString(project.Variables["XmlText"].Value);
return project.Xml.PurchaseOrder.Address[0]["Type"];

Example 2:

var list = new List<string>();
for(int i = 0; i < project.Xml.PurchaseOrder.Address.Count; i++)
{
	list.Add(project.Xml.PurchaseOrder.Address[i].Name.Value);
}
return string.Join(", ", list);

Example 3:

var list = new List<string>();
foreach(dynamic i in project.Xml.PurchaseOrder.Address)
{
	list.Add(i.Name.Value);
}
return string.Join(", ", list);

JSON data can be processed in the same way. Note that properties can be accessed without “Value” attribute.

Example:

return project.Json.employees[1].firstName;

XPath/JsonPath tester

This tool can be used to create proper XPath/JsonPath expression:

en/json_xml.txt · Last modified: 2016/11/25 16:10 by vladz