Skip to main content

Data acquisition and processing

Data is an inevitable product of the development of information technology. It is a common Task for RPA process to collect, sort, process and analyze data. This chapter takes the common operations of data as the main line, and introduces the methods of data acquisition and processing, covering the acquisition of different data sources such as web page data, application data, file data, as well as the processing methods of JSON, strings, Regular Expression, collections, arrays, and other data.

Data acquisition method

Scraping

In the process of RPA, it is often necessary to obtain a set of data from a web page or a table. For example, after we open an e-commerce website in the browser and search for a product, we hope to find Each The names and prices of the goods are preserved. The product name and price are all UI elements, You can use Laiye Automation Platform's "UI elements Automation", go to the web page one by one to select UI elements (product name, price, etc.), and then use commands such as "Get Text" to get the contents of each item. But it is obviously very cumbersome, and it will be difficult to deal with when the number of commodity types found is not fixed in advance. In fact, Laiye Automation Platform provides the function of "Scraping", which can read out multiple groups of associated data in one array with one command. Let's see how to use this function:

Open the Laiye Automation Creator, enter the process block editor, click the "Scraping" button on the toolbar, and an interactive guided dialog box will pop up, which will guide the user to complete the Scraping of the web page. According to the first step of the dialog box, Laiye Automation Platform currently supports Scraping of four programs: table of desktop program, Java table, SAP table, and web page. This paper takes Scraping of web pages as an example to illustrate that Scraping of other three programs has no significant difference in operation.

Start fetching data - select target

Click the "Select Target" button, which is consistent with the "Select Target" button in the "UI elements Automation" we learned earlier. It should be noted that Laiye Automation Platform does not automatically open the web pages and pages you want to crawl, so you need to open data web pages or desktop program forms in advance before data crawl. This can be done manually or through other command combinations. For example, what is demonstrated here is to capture the mobile product information on an e-commerce website. We can use the "Start New Browser" command of "Browser Automation" to open the browser and open the website, use the "Settings Element Text" command to enter "Mobile Phone" in the search bar, and use the "Click Target" command to click the "Search" button. The above steps are described in the guide for junior developers, and will not be explained.

After the web page is ready, the next Task is to locate the data to be captured in the web page, first capture the name of the product, and carefully select the target of the product name (red box and blue background mask box).

Select Product Name

As you can see, a prompt box pops up: "Please select data with the same level to fetch again". You may wonder: What is hierarchical data? Why do you want to grab it again? This is because what we want to capture is a set of data, and we must find the common features of these sets of data. After the target is selected for the first time, a feature is obtained, but it is still unknown which features are common to all targets and which features are only the features of the first target. Only by selecting another data with the same level and grabbing it once again can the commonness of all targets be preserved and the respective characteristics of each target be removed. Just like in mathematics, two points can determine a straight line. We can only select two data to determine which column of data to grab.

Prompt: select the data with the same level to fetch again

To locate the data to be captured, we first capture the name of the product and carefully select the target of the product name (red box and blue background mask box).

Once again, locate the data to be captured in the web page, that is, the name of the product. The first time we capture the name of the first product, this time we capture the name of the second product. Here, you must carefully select the target of the product name to ensure that the target of the same level is captured the second time and the first time, because sometimes there are too many levels in the Web page, and the same text property may nest several levels of targets. If an error is reported at this time, it is usually caused by the wrong target selection. In addition, you can also select the names of the third and fourth commodities for fetching. These do not affect the data fetching results, as long as they are at the same level.

Select the trade name again

After the two targets are selected, a guide box pops up again, asking "Just grab the text or the text link together", and you can select as needed.

Prompt: The data you want to capture is

After clicking the "OK" button, Laiye Automation Platform will give a preview UI of Scraping results. You can check whether the Scraping results are consistent with your expectations: if not, you can click the "Back" button to restart Scraping; If they are consistent, and you only want to capture the "Product Name" group of data, click the "Next" button; If you want to capture more groups of data, for example, the price of goods, you can click the "Capture More Data" button, and the target selection UI will pop up again.

Preview Grab Results - Grab More Data

For example, this time we choose the commodity price text property.

Select commodity price

After selecting the target twice and previewing the data capture results again, we can see that the product name and price have been successfully captured. Moreover, the two sets of data are one-to-one, and the commodity name in the first column corresponds to the price in the second column.

Preview the grab results again

By recycling this method, you can also further increase multiple groups of data to be captured, such as the seller name and evaluation quantity of the product. If you no longer need to capture more data, click the "Next" button. What is the meaning of the guidance page that appears asking "Whether to crawl pages"? Assuming that the web page data is regarded as a two-dimensional data table, the previous step is to increase the number of columns in the data table, such as product name and price, while page flipping is to increase the number of rows in the data table. If only the first page of data is retrieved, click Finish; If you need to capture the data in the next few pages, click the "Capture Page" button.

Grab Flipping

Click the "Grab Page Turning" button to pop up the "Target Selection" guide box. Select the page turning button in the Web page, where the page turning button is ">" Symbol button.

Select Flip Button Target

When all steps are completed, a "Scraping" command will be automatically inserted in the command assembly area of the Laiye Automation Creator, and all attribute of the command have been filled in through the guidance box. Most attribute do not need to be modified, and individual attribute can be further adjusted: "Number of pages to crawl" attribute refers to the number of pages to crawl; The "Number of returned results" attribute limits the maximum number of returned results per page. - 1 indicates unlimited number; The "Page turning interval" attribute refers to how many milliseconds a page is turned. Sometimes the network speed is slow and the interval is longer before the page can be fully opened.

General document processing

In addition to web page Scraping, "file" is another very important data source. Laiye Automation Platform provides several formats for reading files, including general files, INI format files, CSV format files, etc. Let's take a look at general files first.

General file processing is usually used to read and write text files without a specific format. For example, files written with Windows' own "Notepad" belong to this type. In addition, general file processing also includes functions such as determining whether a file exists and which files are in a folder. This section only describes the function of reading text files.

In the command list, find File Processing, expand General File, find the Read File command, and double-click or drag to insert the command. The first attribute of the command is "File Path". You can fill in the path of the file to be read. It can be an absolute path or a relative path. If the absolute path is adopted, click the folder shape icon directly behind to select; If relative path is adopted, it is recommended to switch to professional mode and enter relative path @res"test.txt" , that is, the test.txt file in the res folder under the folder where the process is located. I have already learned the relative path representation.

Special attention should be paid to the second attribute, namely "Character Set Code". Even text files with the same content have different encoding formats, including ANSI/GBK, UTF-8, Unicode, and so on. In Laiye Automation Platform, we generally use UTF-8 encoding. If the read text file is of other encoding, as long as you select the encoding correctly here, Laiye Automation Platform will automatically convert it to UTF-8 for subsequent processing. If you do not know the difference between these codes and which code is used for the file you want to read, there is a lot of information available on the Internet for reference, so this article will not repeat it.

read file

The "Read File" command will read all the files All and put them in a variable of string type. If you need to perform more detailed operations on formatted text files, you can select specific file operation commands according to the file type, such as INI files, CSV files, and so on.

INI file processing

INI file is also called initialization configuration file. Most Windows system programs use this file format to manage various configuration information of the program. The INI file format is relatively fixed, and generally consists of multiple sections. Each section consists of some configuration items, which are key value pairs.

Let's look at the most classic INI file operation: "read key value". Select and insert a "Read Key Value" command under the "INI Format" command Category of "File Processing" in the command center. This command can read the value of the specified key under the specified section in the specified INI file. This command has five attribute in total: "configuration file" attribute, fill in the path of INI file to be read, and fill in the relative path here @res"test.ini" , It indicates that the name of the folder where the process is located is read res Under the folder of test.ini Document, the contents are as follows:

[meta]
Name = mlib
Description = Math library
Version = 1.0

[system]
Libs=sysLibs
Cflags=sysCflags

[user]
Libs=userLibs
Cflags=userCflags

The "section name" attribute fills in the search range of key value pairs. Here, "user" is filled in, indicating that [user] Section to find key value pairs; The "Key Name" attribute is used to fill in the name of the "key" to be searched. Here, "Libs" is filled in, indicating that the content in the form of "Libs=" should be searched; The "Default Value" attribute refers to the default value returned when the key cannot be found; The "Output to" attribute fills in a string variable sRet, and sRet will save the found key value. Note that in the following figure, since we use the "Normal Mode" for input in the "Section Name" and "Key Name" attribute columns, we can directly input string content without adding double quotation marks. If you switch to "Expert Mode", you need to add double quotation marks.

Read INI file

Add a "Output Debugging Information" command and print out sRet. After running the process, you can see that the value of sRet is "userLibs".

CSV file processing

CSV files store table data in plain text, and each line of the file is a data record. Each data record consists of one or more fields separated by commas. CSV files are widely used to exchange data table information between Applications with different architectures to solve the interoperability problem of incompatible data formats.

In Laiye Automation Platform, you can use the "Open CSV File" command to read the contents of the CSV file into the data table, and then perform data processing based on the data table. See the next section for data table processing methods.

Let's first look at the "Open CSV File" command, which has two attribute: the "File Path" attribute fills in the path of the CSV file to be read, and the relative path is filled in here @res"test.csv" , It indicates that the data read from the folder where the process is located res Under folder test.csv Documentation; The "Output to" attribute fills in a data table object objDataTable. After running the command, the contents of the test.csv file will be read into the data table objDataTable. We can add a "Output debugging information" command to view the contents of the objDataTable data table.

Open CSV file

Let's look at the "Save CSV File" command, which also has two attribute: the "Data Table Object" attribute fills in the data table objDataTable obtained in the previous step; The "File Path" attribute fills in the path to save the CSV file. Here is @res"test2.csv" , It is still a relative path, and its meaning has been explained many times, so it will not be repeated.

PDF file processing

In the office scene, PDF files are the most commonly used file formats other than Office files, so the processing of PDF files is also very important. Laiye Automation Platform provides support for PDF file processing. The supported commands are as follows:

PDF Command List

In the command list, under "PDF Format" Category of "File Processing", select and insert a "Get Total Pages" command to get the number of pages of the specified PDF file. This command has three attribute in total: "File Path" attribute. Fill in the path of the PDF file to be read. Here, fill in @res"PDF.pdf" , It indicates that the data read from the process folder res Under folder PDF.pdf Documentation; "Password" attribute, filled with PDF.pdf The opening password of the file. If there is no password, the default value can be maintained. After running this command, the variable name filled in the "Output to" attribute will save the page number of the PDF file.

Get the total pages of PDF file

Laiye Automation Platform can also convert a single page of PDF into a picture file, select and insert a "Save Specified Page as Picture" command, which has five attribute in total: "File Path" attribute and "Password" attribute have the same meaning as the "Get Total Pages" command; The "start page number" and "end page number" attribute specify the start and end page numbers of the PDF file. Fill in 1 and 2 here to indicate the conversion from page 1 to page 2; The "Save directory" attribute is used to fill in the path to save the converted image @res"" , Description After conversion, the picture is saved to the folder where the process is located res Folder. After running, two files will be generated automatically: PDF_1.png and PDF_2.png , They are all picture files, which are the contents of page 1 and page 2 respectively.

Save the specified page as a picture

In addition to processing a single PDF file, Laiye Automation Platform can combine multiple PDF files into one PDF file, select and insert a "Merge PDF" command, which has two attribute: "File Path" attribute fills in the multiple PDF file paths to be merged. Since it is a merged file, it is necessary to have more than one file to have the meaning of merging. Therefore, you must enter the paths of multiple PDF files, that is, you need to fill in an array [@res"PDF.pdf", @res"PDF1.pdf"] , It indicates that the consolidated process res Under folder PDF.pdf Documentation and PDF1.pdf Documentation; The "Save Path" attribute fills in the path of the merged PDF file. The relative path is still filled in here @res"PDF2.pdf" . After running, the two PDF files can be merged into one. In the merged file, the order of the original two PDF files is consistent with their order in the array.

Merge PDF

Data processing method

When the data is read, the next step is to process the data. According to different data formats, Laiye Automation Platform provides different data processing methods and commands, including data tables, strings, collections, arrays, times, or Regular Expression. These methods are described below.

Array

We are Foreword You have learned how to use arrays to store multiple data in. After constructing an array, Laiye Automation Platform also provides a series of commands to process it. Including array editing (adding elements, deleting elements, intercepting and merging arrays), array information acquisition (length, subscript, etc.), etc. For example, you can find the "Array" Category of "Data Processing" in the command list of the Laiye Automation Creator, select and insert a "Add Element at the End of Array" command, which can add an element at the end of the array. This command has three attribute: "target array" attribute, fill in the array before adding elements, and fill in here ["1", "2"] ; The "Add Element" attribute is used to fill in the element to be added, which is filled in here "3" ; The "Output to" attribute saves the added array variable. After adding, use "Output Debugging Information" to display the content of this variable. The expected output result is ["1", "2", "3"] .

Add elements at the end of the array

Let's also look at the "Filter Array Data" command, which can quickly filter the elements in the array, leaving or eliminating the elements that meet the conditions. This command has four attribute: "target array" attribute. Fill in the array to be filtered. Here is ["12", "23", "34"] ; The "Filter Content" attribute is filled in by what criteria to filter the array. Here you fill in "2" , Indicates that the array element is only a string and contains "2" Meet the conditions; There are two options for the "Keep Filter Text" attribute. "Yes" means that the qualified array elements will be retained and the unqualified elements will be eliminated; "No" means that the qualified array elements will be rejected and the unqualified elements will be retained.

You can try it. Select Yes for the attribute of Keep Filtered Text, and output the filtered array variable arrRet. The output result is [ "12", "23" ] , Visible contains a string "2" The array elements of are reserved; If "No" is selected for the "Keep Filter Text" attribute, the output result of the filtered array variable arrRet is [ "34" ] , Include String "2" The array elements of are eliminated.

Filter array data

Set

A set can be regarded as a special one-dimensional array. There are two main differences between it and an array:

  1. The elements in the array can be repeated, but the elements in the collection cannot be repeated. For example, [1, 2, 2, 3] It is a common array, but if it is converted to a collection, one of them will be eliminated 2 , Keep Only 1, 2, 3 These three elements.
  2. The elements in the array are ordered, while the elements in the collection are unordered. For example, add elements to an array in turn 1, 2, 3 , Add to another array 3, 2, 1 , The result will be two different arrays. If you add elements to two sets in turn 1, 2, 3 and 3, 2, 1 , The two sets are still equivalent.

We first try to create a collection. In the command list, find the "Set" Category under "Data Processing", select and insert a "Create Set" command. This command has only one "output to" attribute. It will create an empty set and put this set into the ObjSet variable. If you are familiar with the source view of the Laiye Automation Creator, here is another technique: you can switch to the source view and treat an array as Set.Create The command input can directly convert this array into a collection. If you are not familiar with the source view, subsequent versions will allow you to convert arrays into collections in the visual view.

After creating a collection, you can continue to insert elements into the collection. Use the "Add Element to Set" command, which has two attribute: the "Set" attribute fills in the set ObjSet created in the previous step; The "Add Element" attribute fills in set elements, which can be numbers, strings, etc., or variables.

Can there be both numeric elements and string elements in the same set? The answer is yes! We can call the "Add Element to Collection" command twice, insert 1 once, insert "2" once, and output debugging information. We can see that both elements are successfully inserted into the collection.

If you have created multiple sets, you can also calculate their intersection and union (these concepts are explained in the junior high school math textbook, and if you are not familiar with them, you can ignore this paragraph). Take the union of sets as an example. Two collections are constructed by inserting elements, one is {1, "2"} , The other is {"1", "2"} . Adds a Union command. This command has three attribute: the "set" attribute and the "comparison set" fill in the two sets to be merged respectively; The "Output to" attribute fills in the merged set variable. Output debugging information. You can see that after merging, the collection becomes {1, "1", "2"} , which indicates that union removes repeated elements "2" , 1 and "1" One is a numeric value, and the other is a string, which is not a duplicate element. Therefore, the union is selected at the same time.

The key source code of the above content is as follows:

ObjSet=Set.Create()
Set.Add(ObjSet,1)
Set.Add(ObjSet,"2")
TracePrint(objSet)

ObjSet2=Set.Create()
Set.Add(ObjSet2,"1")
Set.Add(ObjSet2,"2")

objSetRet = Set.Union(ObjSet,ObjSet2)
TracePrint(objSetRet)

After creating a set, or calculating the intersection and union, you can also convert the set to a common array. Laiye Automation Platform provides a command to implement this function. Please practice it yourself.

Data Table

We are Foreword We learned the concept of two-dimensional array in. The data table can be regarded as a special two-dimensional array, but it has many functions than the ordinary two-dimensional array, such as including the header, sorting, filtering and other practical operations.

Let's take an example to see how to build a data table. Find the Category of Data Table command of Data Processing in the command list, select and insert a "Build Data Table" command. This command can generate a data table through the header and build data. This command has three attribute: "Table Column Header" attribute, which is used to represent the header of the data table. A one-dimensional array can be filled in. What we fill in here is ["Name", "Subject", "Score"] ; Next is the "Build Data" attribute. You can fill in a two-dimensional array to represent the initial data in the data table. Here is the [["Zhang San", "Chinese", "78"],["Zhang San", "English", "81"],["Zhang San", "Mathematics", "75"],["Li Si", "Chinese", "88"],["Li Si", "English", "84"],["Li Si", "Mathematics", "65"]] . You can also choose not to use the header or the initial data, and enter it in the corresponding attribute null OK.

Build Data Table

In this way, the data table is built and stored in the variable objDatatable filled in the "Output to" attribute. This data table actually represents the following table. Similar tables are often encountered in the office field, so you can draw inferences from one instance:

NameSubjectScore
Zhang SanChinese78
Zhang SanEnglish81
Zhang SanMathematics75
Li SiChinese88
Li SiEnglish84
Li SiMathematics65

After the data table is built, you can read, sort, filter and other data operations based on the data table. First, let's look at the data sorting operation. Insert a "Data Table Sort" command, which has four attribute: "Data Table" attribute fills in the data table to be sorted, and here fills in the data table object objDatatable obtained in the previous step; The "Sort Column" attribute indicates which column to sort by. Here, "Account" is filled in; The "Ascending Sort" attribute refers to the sort method. "Yes" means ascending, and "No" means descending.

Sort Data Table

The "Output to" attribute is used to fill in the sorted data table object. Here, objDatatable is still filled in. Use the "Output Debug Information" command to view the sorted data table, as shown below:

OrderNameSubjectScore
2Zhang SanMathematics75
5Li SiMathematics65
1Zhang SanEnglish81
4Li SiEnglish84
0Zhang SanChinese78
3Li SiChinese88

Then look at data filtering. Insert a "Data Filtering" command, which has four attribute: "Data Table" attribute fills in the data table to be filtered, and here fills in the data table object objDatatable obtained in the previous step; The "Filter Criteria" attribute refers to the data that meets the filter criteria. Click the "More" button on the right of the attribute column to pop up the "Filter Criteria" input box. Filter criteria include a combination of Column, Condition and Value, for example "Subject Equal to 'Chinese'" , It means to filter out all data whose account is "language". We can add filter criteria. Multiple filter criteria can be "And" or "Or".

Data filtering

Data Filter Criteria

Use the Output Debug Information command to view the filtered data table as follows:

OrderNameSubjectScore
0Zhang SanChinese78
3Li SiChinese88

The data table can also be converted into a two-dimensional array. Laiye Automation Platform provides a command to achieve this function. Please practice it yourself.

String

String is the most common data type, and string operation is also the most common data operation. Mastering string operations will benefit from subsequent development. Let's take a look at the most classic command: "Find String". This command will search whether the specified character exists in the string. This command has five attribute: "target string" attribute fills in the searched string, and here is "abcdefghijklmn" ; The "Search Content" attribute is filled with the specified characters to be searched. Here is "cd" ; The "Start Search Location" attribute refers to the location from which to start searching, and the starting position is 1; The case sensitive attribute refers to whether case is sensitive when searching. The default value is No; The "Output to" attribute fills in a variable iRet, which saves the character positions found. Run the command to display the value of the variable iRet. The output result is 3, indicating that "cd" Appears in "abcdefghijklmn" 3rd bit of. If the string to find does not exist, the output result will be 0.

Find String

Let's look at a common string operation: the "Split String" command. This command splits a string into an array using a specific delimiter. For example, you can use this command to process the CSV format file mentioned earlier, because there is a clear delimiter in the CSV format file. The command has three attribute: "target string" The attribute fills in the string to be split, and here it is filled in "zhangsan|lisi|wangwu" ; The "Delimiter" attribute is filled with the symbol used to divide the string. Here is "|" ; The "Output to" attribute saves the split string array to arrRet. To view the results, let's add another "Output Debugging Information" command to output the value of the variable arrRet. You can see that the result is [ "zhangsan", "lisi", "wangwu" ] , Indicates a string "zhangsan|lisi|wangwu" By separator "|" , Was successfully split into a string array [ "zhangsan", "lisi", "wangwu" ] .

Split String

Regular Expression

When writing a string processing process, it is often necessary to find and test whether a string conforms to certain complex rules. Regular Expression is a tool used to describe these complex rules. It can not only easily find and test single string data, but also handle a large amount of data (such as data collection, web crawler, etc.).

First, let's look at the "Regular Expression Search Test" command. This command attempts to use Regular Expression to find a string. If it can be found, the result is true. If it cannot be found, the result is false. This command can be used to determine whether a string meets a certain condition. The command has three attribute: the "target string" attribute fills in the string to be tested; Fill in Regular Expression for "Regular Expression" attribute; The "Output to" attribute saves the test results. For example, the website judges whether the registered user name entered by the user is legal. First, it writes the judgment conditions of the legal user name into Regular Expression, and then uses Regular Expression to test whether the string entered by the user meets the conditions. Specifically, the "Regular Expression" attribute is filled "^[a-zA-Z0-9_-]{4,16}$" , Indicates that the registration name is 4 to 16 characters, and the characters can be upper and lower case letters, numbers, underscores, and horizontal lines; "Target string" if filled "abc_def" , The test result is True , explain "abc_def" Conform to Regular Expression. "Target string" if filled "abc" or "abcde@" , The test result is False , because "abc" The length of is 3, "abcde@" Contains characters "@" , They do not conform to the Regular Expression rules.

Note: In the above Regular Expression "^" Symbol and ending "$" The symbol represents matching to the beginning or end of the string. With these two symbols, the string to be tested must Match All Regular Expression will get True Results of. If these two symbols are not available, the string to be tested only contains the matching Substring , Will get True Results of.

Regular Expression Lookup Test

Let's look at the "Regular Expression Find All" command. This command uses Regular Expression to find strings and find all strings that meet the conditions. The command also has three attribute: the "target string" attribute fills in the string to be searched; Fill in Regular Expression for "Regular Expression" attribute; The "Output to" attribute stores all the substrings found in the form of an array. For example, the "target string" attribute fills in a page crawled back by the web crawler, as shown below:

<p/>
<img src="https://avatar.csdn.net/A/4/C/3.jpg"/>
<p/>
<img src="https://g.csdnimg.cn/static/1x/11.png"/>
<p/>

Fill in "Regular Expression" attribute https?://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|] This Regular Expression looks very complicated. In fact, it is not written by me. It is a Regular Expression I found on the Internet that can be used to match URLs. With this Regular Expression, you can extract all All links in the crawled back web page.

After configuring the attribute, select this command line, click the triangle on the right, and run this command line separately to automatically output the results. You can see that the result is:

arrRet = [
"https://avatar.csdn.net/A/4/C/3.jpg",
"https://g.csdnimg.cn/static/1x/11.png"
]

Both URLs have been successfully extracted!

The use of the Regular Expression command itself is not difficult, but the difficulty lies in how to write Regular Expression. For this aspect, there are already a large number of courses This article will not repeat.

JSON

JSON is a lightweight data exchange format for storing and exchanging text information. JSON is easy to be read and written by people, and also easy to be parsed and generated by machines. JSON is similar to XML in use, but smaller, faster, and easier to parse than XML.

Laiye Automation Platform has two JSON commands, one is "JSON string to data", the other is "data to JSON string". The data here refers to the data in array and dictionary format in Laiye Automation Platform. That is, through these two commands, you can convert JSON strings to arrays and dictionaries in Laiye Automation Platform in both directions. The array in Laiye Automation Platform corresponds to the Array in JSON , The dictionary in Laiye Automation Platform corresponds to the object in JSON .

Let's start with the "JSON string to data" command, which can convert JSON strings to arrays or dictionaries in Laiye Automation Platform. This command has two attribute: "conversion object" attribute, fill in JSON string, here fill in { "Name": "Zhang San", "Age": "26" } . "Output to" attribute, fill in the converted Laiye Automation Platform variable, for example, we fill in objJSON here.

JSON string conversion to data

Use the "Output Debug Information" command to output the results: { "Name": "Zhang San", "Age": "26" } . Some readers may wonder, what does the input and output look like? Actually not! Note that in the above figure, when we fill in the JSON string, we use the Normal Mode to enter it (the Exp button on the left is gray). In fact, Laiye Automation Platform will change it into a string after necessary escape. Switch to Expert Mode (click Exp button to turn it into blue), and you can see the details of this string. The output is a dictionary in Laiye Automation Platform. Input string and output dictionary are very different in use. For example, the dictionary in Laiye Automation Platform can use objJSON["Name"] To use the value of the corresponding element, and the result is "Zhang San" , String cannot be operated in this way.

Switch to the source code view to try:

TracePrint(objJSON["Name"])

Now that it can be accessed, it should also be able to be modified. Add an assignment statement, which will "Age" It is revised as 30.

objJSON["Age"]="30"

Finally, use the "Convert Data to JSON String" command to convert the modified JSON object to a string. This command has two attribute: the "conversion object" attribute, fill in the array or dictionary to be converted, that is, objJSON that has been used previously; The "Output to" attribute fills in a string variable, which will save the converted JSON string. Use the "Output Debug Information" command to view the converted JSON string: "{ "Name" : "Zhang San", "Age" : "30" } " , It can be seen that the content has been modified successfully.

Let's do a comprehensive experiment: read a file in JSON format, modify the read data, and then write it back. First, we create a new process, and select the process res Create a new one under the folder person.json Document, the contents of which are:

{ "Name" : "Zhang San", "Age" : "30" }

Open the process block, switch to source code mode, and paste the following source code into it.

Dim obj
File.Read(@res"person.json","auto")
obj=JSON.Parse($PrevResult)
obj['Gender']='Male'
JSON.Stringify(obj)
File.WriteFile(@res"person.json", $PrevResult)

This simple process uses the commands of file reading and writing and JSON data processing described in this chapter. After running, open it again person.json After viewing the file, you can see that a field "Gender" has been added to its content, and the value is "Male".

This process is not complicated. Even if it is not annotated, it is not difficult for readers to understand it. It is worth noting that system variables are used twice in this process $PrevResult , To refer to "the result of the previous command". If the output of a command is only used as the input of the next command and will not be used in other places, using this system variable can reduce the definition and use of variables. In addition, when the process is switched to the visual view, it will be more concise and easy to read.

Time

The time operation command mainly includes the conversion between time data and string, as well as various operations on time data. First, let's see how to obtain the current time. Under the Category of Time command in Data Processing in the command center, select and insert a command to obtain the time. This command can obtain the number of days since January 1, 1900. This command has only one "output to" attribute. Save the current time. Fill in dTime here. In the visual view, click the triangle on the right of this command line, run this command line, and the result will be automatically output: 43771.843969907, indicating that 43771.843969907 days have passed since January 1, 1900, and the following decimal places represent the hours, minutes, and seconds of the day. You can roughly estimate whether it is correct.

Sometimes it is easier to save time in this way. For example, to obtain the time after 100 days, you only need to add 100 to the above value, regardless of the carry of year, month and day. Of course, this way of expressing time does not conform to our daily habits and is not suitable for reading. If you need to show the time to others, you can use the Format Time command to convert the time data into strings of various formats. The "Format Time" command has three attribute: the "Time" attribute fills in the just obtained time data dTime; The "Format" attribute fills in the time format, where year (yyyy) occupies 4 digits, month (mm), day (dd), 24-hour (hh), minute (mm), and second (ss) occupy 2 digits, for example "yyyy-mm-dd hh:mm:ss" After filling in the time, it will be converted to: "2019-11-02 20:29:58" ; The "Output to" attribute stores the string of formatting time. Careful readers may have found that with this format, both the "month" and "minute" formats are mm , This is because these two words are respectively "month" and "minute" in English, both of which are written in letters m Beginning. So the format is mm , Laiye Automation Platform will judge according to the context mm Does it mean "month" or "minute". If you want to specify more clearly, instead of letting Laiye Automation Platform judge, you can also use uppercase MM As the format of "month" nn As the format of "minute", the effect is the same.

Format Time

In addition to converting time data into strings of various formats, you can also directly obtain an item of time data. For example, you can use the "Get Month" command to get the month in the time data dTime, and so on. Other commands are similar.