[英]How to Parse Json Object Element that contains Dynamic Attribute
我們有一個 Json object 結構如下。
{
"results": {
"timesheets": {
"135288482": {
"id": 135288482,
"user_id": 1242515,
"jobcode_id": 17288283,
"customfields": {
"19142": "Item 1",
"19144": "Item 2"
},
"attached_files": [
50692,
44878
],
"last_modified": "1970-01-01T00:00:00+00:00"
},
"135288514": {
"id": 135288514,
"user_id": 1242509,
"jobcode_id": 18080900,
"customfields": {
"19142": "Item 1",
"19144": "Item 2"
},
"attached_files": [
50692,
44878
],
"last_modified": "1970-01-01T00:00:00+00:00"
}}
我們需要訪問結果中的元素 --> 時間表 --> 動態 ID。
例子:
{
"id": 135288482,
"user_id": 1242515,
"jobcode_id": 17288283,
"customfields": {
"19142": "Item 1",
"19144": "Item 2"
},
"attached_files": [
50692,
44878
],
"last_modified": "1970-01-01T00:00:00+00:00"
}
問題是 "135288482": { 是動態的。 我們如何訪問其中的內容。
我們正在嘗試創建數據流來解析數據。 數據是動態的,因此無法通過屬性名稱進行訪問。
AFAIK ,根據您的 JSON 結構和動態鍵,使用數據流可能無法獲得所需的結果。 我已經復制了上面的內容,並且能夠像下面這樣使用 set variable 和 ForEach 來完成它。
在您的 JSON 中, id 的鍵和值是相同的。 所以,我首先用它來獲取鍵列表。 然后使用該密鑰列表,我能夠訪問內部 JSON object。
這些是我在管道中的變量:
首先,我有一個字符串類型的集合變量,並在其中存儲了"id"
。
然后我進行了查找活動以從 blob 中獲取上述 JSON 文件。
我使用下面的動態內容@string(activity('Lookup1').output.value[0].results.timesheets)
將查找時間表對象存儲為設置變量中的字符串
我在帶有"id"
的字符串上使用了 split,並將結果數組存儲在數組變量中。 @split(variables('jsonasstring'), variables('ids'))
這將給出如下所示的數組。
現在,我對這個數組使用了一個Foreach ,但跳過了第一個元素。 在每次迭代中,我將字符串的前 9 個索引取到 append 變量,這是關鍵。 如果您的 id 值和鍵不相同,那么您可以跳過拆分數組中的最后一個,並根據您的要求從字符串的反面獲取鍵。 這是我在 ForEach @take(item(), 9)
中的 append 變量活動的動態內容
然后我拿了另一個 ForEach,並給它這個鍵列表數組。 在 foreach 中,您可以使用以下動態內容訪問 JSON。 @string(activity('Lookup1').output.value[0].results.timesheets[item()])
這是我的管道 JSON:
{
"name": "pipeline1",
"properties": {
"activities": [
{
"name": "Lookup1",
"type": "Lookup",
"dependsOn": [
{
"activity": "for ids",
"dependencyConditions": [
"Succeeded"
]
}
],
"policy": {
"timeout": "0.12:00:00",
"retry": 0,
"retryIntervalInSeconds": 30,
"secureOutput": false,
"secureInput": false
},
"userProperties": [],
"typeProperties": {
"source": {
"type": "JsonSource",
"storeSettings": {
"type": "AzureBlobFSReadSettings",
"recursive": true,
"enablePartitionDiscovery": false
},
"formatSettings": {
"type": "JsonReadSettings"
}
},
"dataset": {
"referenceName": "Json1",
"type": "DatasetReference"
},
"firstRowOnly": false
}
},
{
"name": "JSON as STRING",
"type": "SetVariable",
"dependsOn": [
{
"activity": "Lookup1",
"dependencyConditions": [
"Succeeded"
]
}
],
"userProperties": [],
"typeProperties": {
"variableName": "jsonasstring",
"value": {
"value": "@string(activity('Lookup1').output.value[0].results.timesheets)",
"type": "Expression"
}
}
},
{
"name": "for ids",
"type": "SetVariable",
"dependsOn": [],
"userProperties": [],
"typeProperties": {
"variableName": "ids",
"value": {
"value": "@string('\"id\":')",
"type": "Expression"
}
}
},
{
"name": "after split",
"type": "SetVariable",
"dependsOn": [
{
"activity": "JSON as STRING",
"dependencyConditions": [
"Succeeded"
]
}
],
"userProperties": [],
"typeProperties": {
"variableName": "split_array",
"value": {
"value": "@split(variables('jsonasstring'), variables('ids'))",
"type": "Expression"
}
}
},
{
"name": "ForEach to append keys to array",
"type": "ForEach",
"dependsOn": [
{
"activity": "after split",
"dependencyConditions": [
"Succeeded"
]
}
],
"userProperties": [],
"typeProperties": {
"items": {
"value": "@skip(variables('split_array'),1)",
"type": "Expression"
},
"isSequential": true,
"activities": [
{
"name": "Append variable1",
"type": "AppendVariable",
"dependsOn": [],
"userProperties": [],
"typeProperties": {
"variableName": "key_ids_array",
"value": {
"value": "@take(item(), 9)",
"type": "Expression"
}
}
}
]
}
},
{
"name": "ForEach to access inner object",
"type": "ForEach",
"dependsOn": [
{
"activity": "ForEach to append keys to array",
"dependencyConditions": [
"Succeeded"
]
}
],
"userProperties": [],
"typeProperties": {
"items": {
"value": "@variables('key_ids_array')",
"type": "Expression"
},
"isSequential": true,
"activities": [
{
"name": "Each object",
"type": "SetVariable",
"dependsOn": [],
"userProperties": [],
"typeProperties": {
"variableName": "show_res",
"value": {
"value": "@string(activity('Lookup1').output.value[0].results.timesheets[item()])",
"type": "Expression"
}
}
}
]
}
}
],
"variables": {
"jsonasstring": {
"type": "String"
},
"ids": {
"type": "String"
},
"split_array": {
"type": "Array"
},
"key_ids_array": {
"type": "Array"
},
"show_res": {
"type": "String"
}
},
"annotations": []
}
}
結果:
在動態內容中使用@json()
將以下字符串轉換為 object。
如果要將此 JSON 存儲在文件中,請使用 ForEach 並在 Foreach 內部使用 SQL 腳本在每次迭代中使用JSON_VALUE
將每個 object 作為一行復制到表中。 然后在 Foreach 外部使用復制活動根據您的要求將 SQL 表復制到您的目的地。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.