Skip to main content

Node - JSON Parsing


Function: For JSON objects output in the [Code block] node or [Sending API requests] node, with [JSON parsing] node, you can output the specified content as dynamic parameters (variables or arrays) via JSONPath expressions for subsequent nodes.

  • Processable data: JSON objects returned from the [Code block] node or the [Sending API requests] node.

  • Exportable data: Variables or arrays

Scenario: Through the API of logistics query platform, the historical logistics information of a certain express can be obtained and written to the sub-form of logistics information, and the latest logistics information obtained needs to be written to the current field. According to the JSON object of logistics information, the object data of logistics history information and the single latest logistics information can be obtained and output separately by JSONPath expression for the subsequent batch adding and updating records nodes.

Configuration

1. Get the logistics information by [Sending API requests] node

As shown in the following figure, the logistics information is obtained according to the express number and returned through the JSON object.

2. Add the node – [JSON parsing]

3. Get the JSON object

That is, select the node that outputs the JSON object. Here the node [Get logistics information] (API) is selected.

4. Check the parsing results

  • 1) Click the checking button

  • 2)Result description

    In the parsing results there is a description of the JSON object returned by the API, including the corresponding reference values and JSON Path expressions.

5. Define the output parameters

  • 1) Select parameters from the parsing result

    For example, we need to get all logistic information and the first piece of logistic information. Click [Generate parameter] on the right, and the corresponding parameters will be added automatically.

    If you are more familiar with JSONPath expressions, you can add parameters manually.

  • 2) Example of output parameters

    After defining the output parameters, the corresponding example data will be displayed below immediately.

6. Define the error message

When returning a JSON that matches an error result, you can customize the error message when stopping outputting parameters.

  • You can use the return value obtained in the output parameter as an error message, comparing the conditions of one or more values to determine the error.

  • Error messages can be fixed text or dynamic values selected from other nodes.

  • If you define an error message, you can define whether the execution of subsequent nodes continues when an error occurs.

    • Continue execution

      Continue the execution of the subsequent nodes. If a subsequent node involves object or output data in this node, the corresponding item is skipped. (For example, if 5 fields are updated through the update node and one filed involves the value of this node, then only this field is not processed and the other fields are updated normally).

    • Stop execution

      The workflow ends at this node and the subsequent nodes are not executed.

7. Use parameters output by [JSON parsing] node

1) Add the logistics information to the worksheet in bulk

  • Get the logistics array information

    The output array must first be received with the [Get multiple pieces of data] node and converted to a data structure that can be recognized by the platform.

  • Write data in bulk with [Add new records] node

2) Write the latest logistics information to the fields of this worksheet

Non-array information can be selected directly from the node.

What is JSONPath

JSONPath is a tool for parsing JSON formatted documents based on XPath. With JSONPath expressions, you can get any specified data from a JSON structure. The [JSON Parsing] node in Mingdao can parse incoming JSON data with JSONPath and output it as parameters for subsequent nodes.

JSONPath and XPath

As anyone who knows XML knows, one of the outstanding advantages of XML is that there are tools available to parse, transform, and selectively extract data from XML documents. XPath is one of these powerful tools. The JSONPath expression is a similar JSON tool. It can describe JSON structures directly, in the same way that XPath expressions refer to XML documents. Since JSON structures are usually anonymous and do not have a "root member object", JSONPath uses the name $ to denote the outermost object.

For example, we use an XPath to describe getting the title of the first book under the store node from the XML structure under the root node: /store/book[1]/title For the same JSON structure, we can use the following JSONPath to get that data: $.store.book[0].title or $['store']['book'][0]['title']. The following is a comparison of the usage of the JSONPath syntax element with the XPath element.

Filter Operators

A filter expression can be used in JSONPath to filter out the eligible nodes. A filter expression returns a Boolean value, and JSONPath filters the eligible nodes into an array and returns them. A typical filter expression is [? (@.age > 18)], where @ represents the current node being processed. More complex filtering expressions can be created using the logical operators && and ||. Strings must be enclosed in single or double quotation marks, e.g. [? (@.color == ‘blue’)] or [? (@.color == “blue”)]. The available operators are shown in the following.

Example of JSONPath

The following code represents a series of items information.

  • There are 3 books and a bicycle.
  • Each book has a category, author, title and price.
  • The bicycle has a color and a price.
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}

Then, using JSONPath, you can get the following results.


Have questions about this article? Send us feedback