Processing JSON/XML

Table of contents


Description

The action is used to process and work with JSON and XML.

Only one JSON and one XML object can be processed in a project at a time. If you need to process several pieces of data of the same format, then this will need to be done in turn.

JSON

JSON (JavaScript Object Notation) is a simple data exchange format that is easy to read and write by both humans and computers.

Example:

{ "firstName": "Jhon", "lastName": "Doe", "address": { "streetAddress": "8478 Rockledge Street", "city": "New York", "postalCode": 10306 }, "phoneNumbers": [ "555 123-1234", "555 987-6541" ] }

XML

XML - (eXtensible Markup Language) is a markup language that resembles HTML. Designed to transmit data, not display it. XML tags are not predefined. It is up to you to define the tags you want.

<CATALOG> <CD> <TITLE>Imperial Parody</TITLE> <ARTIST>Bob Dylan</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Colombia</COMPANY> <PRICE>10.90</PRICE> <YEAR>1985</YEAR> </CD> <CD> <TITLE>Hide your heart</TITLE> <ARTIST>Bonnie Tyler</ARTIST> <COUNTRY>United Kingdom</COUNTRY> <COMPANY>CBS Entries</COMPANY> <PRICE>9.90</PRICE> <YEAR>1988</YEAR> </CD> <CD> <TITLE>Top Hits</TITLE> <ARTIST>Dolly Parton</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>RCA</COMPANY> <PRICE>9.90</PRICE> <YEAR>1982</YEAR> </CD> </CATALOG>

Example 2:

It happens that services do not return beautifully formatted text (as in the examples above), but everything in one line, which makes reading much more difficult. In such cases, you can use the so-called beautifiers. The X/JSON Path Tester already has this component built in (Beautify button)

For an action, it doesn't matter if the text is written in one line or is nicely formatted - the main thing is that it is correctly composed.

How to add an action to a project?

Via context menu Add Action -> Data -> Processing JSON / XML

Or use smart search.

Where is JSON / XML used?

Most often, these formats are used in the API of various services.

For example, services for recognizing captcha or SMS services (if you work with them directly using GET, POST and other requests, bypassing the Services for SMS processing and Recognize captcha actions) usually work with at least one of these formats (or with two at once)

How to work with an action?

First, you should choose the type of data with which you will work.

Parsing

The data needs to be processed, the Parsing action is responsible for this.

Variable window

The result of the action execution is entered into the Json or Xml project variable, respectively, the content can be viewed in the Variables Window

Since XML was parsed in the example, the tab in the Variables window was selected accordingly.

The Count (1) variable stores the number of elements - it is very convenient to use it as a delimiter when traversing data in a loop. If in our example there were still <DVD> tags at the same level with <CD>, then there would be a variable CATALOG.DVD.Count with the number of <DVD> tags

With the help of special buttons (2) you can immediately copy a variable macro to the clipboard.

Variable macros

Here is an example of one of the variable macros - {- Xml.CATALOG.CD [1] .ARTIST-}.

You can use other variables inside this variable, example:

{-Xml.CATALOG.{-Variable.item_type-}[{-Variable.counter-}].{-Variable.property-}-}

To quickly insert a variable, you can press the key combination CTRL + SPACEBAR in any text field, a drop-down menu will appear, in this menu, double-click on Xml or Json (depending on what you are working with), then put a period and another menu will appear already with parsed variables.

Process JsonPath / XPath

This option is used when it is necessary to select a subset of all the data that is processed using the Parsing action. In this case, an XPath expression (for XML) or JsonPath (for JSON) is constructed. The X / JSON Path Tester can help you in composing an expression. You can use variable macros in the field to insert an expression.

XML Features

XML has node attributes. To access them in macros, square brackets with a string value are used. (For example: {-Xml.CATALOG.CD [0] ["item"] -}).

Processing speed

Parsing large JSON or XML while debugging a project in ProjectMaker can take a long time. But when working in ZennoPoster, parsing will be much faster.


Add to list

This action is used when you need to get one specific property from all data for each element. Variables can be used.

Property - indicates the field that will be parsed as an array. It is possible to use nesting here by specifying a dot (for example: store.employees) If the field is not an array, then one element is added to the list.

Subproperty - since arrays can contain complex objects, you can specify which value to take from it for the list.

If we take the data from the Description section as a basis and imagine that we only need years for each CD, then this action could look like this:


Add to table

This action is similar to the previous one, but here you can get not one property, but several. Variables can be used.

Columns are named as in Excel - capital (!) Latin letters, in alphabetical order. If any column is missed, it will be empty. In the screenshot, you can see that column C is not indicated, this is how it affected the final table:


Working in code

In C#, JSON and XML objects are located in the project object. They are of type dynamic. Because of this, the code editor cannot fully display the dropdown hints.

Sample code for working with XML:

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

Example 2:

Example 3:

The same happens with Json. However, it should be noted that access to properties occurs without using “Value”.

Example:


Usage example

JSON

There is a site http://ip-api.com , which returns detailed information on the ip address using the simplest API. Useful for:

  • to make sure that the project works through a proxy and not through the main ip

  • selection of a country / city during registration, according to proxy data

It can return data in different formats (more details here - https://ip-api.com/docs ), but we are now interested in JSON.

To get information, make a GET request at http://ip-api.com/json then process the result through the Parse action and then work with the received data


XML + XPath Processing

As data we will use what is in the Description section .

Let's imagine that from all the data we only need the album names. For this task, create an XPath expression // CATALOG / CD / TITLE .

Action settings and result of its work (right)