Using Azure Logic Apps, i'm trying to transform JSON array to JSON object. For example, if i have an array as:
[
{
name: 'john'
id: '1'
},
{
name: 'sarah'
id: '2'
},
]
I'm would like to have as output:
{
'1': 'john',
'2': 'sarah'
}
First init Json Result:
Then in foreach loop (TestArray is our array with data) add compose with Expression "addProperty(variables('JsonResult'),item()?['id'],item()?['name'])"
and set variable with output from compose:
In the foreach settings be sure to enable concurrency control, and set the Degree of Parallelism to 1. This is to ensure a sequential run and the variable not being written over in between.
Code for LA:
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"For_each": {
"actions": {
"Add_property": {
"inputs": "@addProperty(variables('JsonResult'),item()?['id'],item()?['name'])",
"runAfter": {},
"type": "Compose"
},
"Set_variable": {
"inputs": {
"name": "JsonResult",
"value": "@outputs('Add_property')"
},
"runAfter": {
"Add_property": [
"Succeeded"
]
},
"type": "SetVariable"
}
},
"foreach": "@variables('TestArray')",
"runAfter": {
"Init_Json_Result": [
"Succeeded"
]
},
"runtimeConfiguration": {
"concurrency": {
"repetitions": 1
}
},
"type": "Foreach"
},
"Init_Array": {
"inputs": {
"variables": [
{
"name": "TestArray",
"type": "array",
"value": [
{
"id": "1",
"name": "john"
},
{
"id": "2",
"name": "sarah"
}
]
}
]
},
"runAfter": {},
"type": "InitializeVariable"
},
"Init_Json_Result": {
"inputs": {
"variables": [
{
"name": "JsonResult",
"type": "object",
"value": {}
}
]
},
"runAfter": {
"Init_Array": [
"Succeeded"
]
},
"type": "InitializeVariable"
},
"Test_result": {
"inputs": "@variables('JsonResult')",
"runAfter": {
"For_each": [
"Succeeded"
]
},
"type": "Compose"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {},
"triggers": {
"manual": {
"inputs": {
"schema": {}
},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {}
}