Skip to main content

Node - JSON Parsing


Node Function: For JSON objects outputted by the "Code Block" node or the "Send API Request" node, the JSON Parsing node can use JSON Path expressions to output specified content as dynamic parameters (variables or arrays) for subsequent nodes to use.

Processed data: JSON objects returned from the Code Block node or the Send API Request node.

Output data: Variables or arrays.

Use Case: To obtain the order information through the API of the order query platform for subsequent nodes, add records in bulk or update records to write order infomation to the worksheet.

Workflow Configuration

1. Add a "Send API Request" node to obtain order information

As shown below, the order information is obtained through an API request, which returns the JSON object.

2. Add the [JSON Parsing] node

3. Get the JSON object

Select the node from which the JSON object is outputted. Here, choose the "Obtain order info (API)" node.

4. View the parsing results

  • 1) Click the "View JSON parsing results" button

  • 2)Result description

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

5. Define the output parameters

  • 1) Select parameters from the parsing results

    Click the "Generate" button on the right, and the parameters will be added automatically.

    If you are more familiar with JSON Path 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 there is 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.

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

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

    • Continue to execute

      Continue to execute the subsequent nodes. If a subsequent node uses the object or output data from this node, the corresponding items will be skipped. (For example, if five fields need to be updated and one filed involves the value from this node, only this field is not processed and the other fields are updated normally).

    • Abort the process

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

7. Use the output parameters

Batch add the order details to the worksheet

  • Get the array information

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

  • Batch add records

What is JSONPath

JSONPath is a tool designed for parsing JSON format documents, similar to XPath. With JSONPath expressions, you can easily obtain any specified data from a JSON structure. In the workflow, the "JSON Parsing" node parses the incoming JSON data using JSONPath and outputs it as parameters for use by subsequent nodes.

JSONPath and XPath

For those familiar with XML, it is well known that one of the frequently emphasized advantages of XML is the abundance of tools available for analyzing, transforming, and selectively extracting data from XML documents. XPath is one of these powerful tools. Similarly, JSONPath expressions are a JSON tool that can directly describe the JSON structure, following a similar format as XPath expressions used for referencing XML documents. As JSON structures typically lack a "root member object" and are often anonymous, JSONPath uses the name $ to represent the outermost object.

For example, if we use an XPath expression to describe retrieving the title of the first book under the store node in the root node of an XML structure: /store/book[1]/title

In the same JSON structure, we can use the following JSONPath to obtain this data: $.store.book[0].title or $['store']['book'][0]['title']

Below is a complete comparison of the syntax elements of JSONPath and the corresponding elements in XPath:

Filter Operators

In JSONPath, filter expressions can be used to filter out nodes that meet certain conditions. Filter expressions need to return a boolean value, and JSONPath will filter out nodes that meet the conditions and return them as an array. A typical filter expression is [?(@.age > 18)], where @ represents the current node being processed. More complex filter expressions can be created using logical operators && and ||. Strings must be enclosed in single quotes or double quotes, such as [?(@.color == 'blue')] or [?(@.color == "blue")].

The available operators are as follows:

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