The JSON data contains multiple array fields, as shown in the following figure.
You want to parse these array fields and align them in a one‑to‑one correspondence, as shown in the following figure.
You can select the JSON field to be parsed in a JSON Parsing operator and modify the JSON path.
In the actual environment, JSON data originates from various sources. This section creates JSON data containing arrays.
1. Create a scheduled task, drag a Data Transformation node onto the page, and enter the Data Transformation editing page.
2. Drag in a Spark SQL operator and configure it to create JSON data containing arrays, simulating the usage scenario, as shown in the following figure.
The SQL script is as follows:
select '{ "header": { "desc": "success", "failures": [], "oprs": 1, "succ": 1, "oprtime": 0, "quota": 1, "rquota": 200000000, "status": 0, "traceid": "a9874ba8-b3a0-11ed-8323-97b36044f232" }, "body": { "data": [ { "result": { "total": 20, "pageSum": [ [ 386, 130, 279, 212, 189, 41.98, 263, 71.06, 230, 8.61, "--", "--" ], [], [] ], "timeSpan": [ "2023/03/01 - 2023/03/02" ], "sum": [ [ 364, 200, 249, 212, 82, 41.98, 253, 71.06, 220, 1.61, "--", "--" ], [] ], "fields": [ "aa", "bb", "cc", "dd", "ee", "ff", "hh", "ii", "er", "ef", "qw", "dg" ] } } ], "expand": {} }}' as json
Drag in a JSON Parsing operator and configure it to parse the JSON data.
1. Select json as the source field, and choose sum and fields nodes.
2. Click Data Preview, as shown in the following figure.
The data after parsing is in an array format. You need to modify the JSON path to correctly parse the array fields so that the generated fields correspond one-to-one.
2. Modify the JSON path, as shown in the following figure.
[0] indicates accessing the first element in an array. In a JSON array, the index typically starts from 0, so [0] means accessing the first element of the array.
[*] is a wildcard character that matches all elements in an array at the current position.
The expression [0][*] accesses the first element in a JSON array and retrieves all its child elements. This is commonly used in nested JSON structures to obtain a collection of child elements from a specific element.
For more details about JSON paths, see https://github.com/json-path/JsonPath.
Click Data Preview, as shown in the following figure.
3. You can continue processing or output the data as needed.
滑鼠選中內容,快速回饋問題
滑鼠選中存在疑惑的內容,即可快速回饋問題,我們將會跟進處理。
不再提示
10s後關閉
Submitted successfully
Network busy