Add properties to an object in a variable in Azure Logic Apps - azure

How can I manage to add, update or delete a property of an object in a variable in Azure Logic Apps?
Example of my object before:
{
"prop1": "value1"
}
Example of my object after:
{
"prop1": "value1",
"prop2": "value2",
}
I would like to use the Set variable operation in order to add a new property in the variable (I used the union function, but with a temporary variable because assigning a self-referenced value to a variable isn't authorized).
Thank you for your help!

You can use an Initialize variable to create the prop2 variable or you can source it from wherever you need to. Then you can use Compose step to combine the prop1 and prop2.
To illustrate, I used a http triggered logic app with a JSON body of "prop1" and value "value1". I think Initialize the variable "prop2" to "value2". For my compose block I used the input
{
"prop1": "#{triggerBody()?['prop1']}",
"prop2": "#{variables('prop2')}"
}
My output is a webhook which will receive the combined JSON as
{
"prop1": "value1",
"prop2": "value2"
}

Related

MAP Nested JSON using rest API in Azure Data Factory using for each Iterator through a stored procedure to a SQL table

I am pulling a list of pipeline using REST API in ADF. The API gives a nested JSON and I am using a for each iterator to be pulled via a stored procedure to be pulled into a SQL table.
The sample JSON looks like this:
{
"value": [
{
"id": "ADF/pipelines/v_my_data_ww",
"name": "v_my_data_ww",
"type": "MS.DF/factories/pipelines",
"properties": {
"activities": [
{
"name": "Loaddata_v_my_data_ww",
"type": "Copy",
"dependsOn": [],
"policy": {
"timeout": "7.00:00:00",
"retry": 0,
"retryIntervalInSeconds": 30,
"secureOutput": false,
"secureInput": false
}
]
},
"folder": {
"name": "myData"
},
"annotations": [],
"lastPublishTime": "2021-04-01T22:09:20Z"
,
"etag": "iaui7881919"
}
}
]
}
So using the For Each Iterator #activity('Web1').output.value, I was able to pull in the main keys, like id, name , type. However I also need to pull in the name/type from within the properties/activities tag. not sure how to do it. I was trying the following.
When I run the pipeline, I get the following error:
The expression 'item().properties.activities.name' cannot be evaluated because property 'name' cannot be selected. Array elements can only be selected using an integer index.
Any help in this regards will be immensely appreciated.
The error is because you are trying to access an array i.e., activities directly with a key. We need to access array elements with indices such as activities[0]
The following is a demonstration of how you can solve this. I got the same error when I used #item().properties.activities.name.
To demonstrate how to solve this, I have taken the given sample json as a parameter for pipeline and passing #pipeline().parameters.js.value in for each activity.
Now, I have used a set variable to show how I retrieved the name and type present in the activities property. The following dynamic content helps in achieving this.
#item().properties.activities[0].name
So, modify your prop_name and prop_type to the following dynamic content:
#item().properties.activities[0].name
#item().properties.activities[0].type
UPDATE:
If there are multiple objects inside activities property array, then you can follow the procedure below:
Create an execute pipeline activity to execute a pipeline called loop_activities. Create 4 parameters in loop_activities pipeline name, id, tp, activities. Pass the dynamic content as shown below:
In loop_activities pipeline, use for each to iterate through activities array. The dynamic content value for items field is #pipeline().parameters.activities.
Inside for each, you can access each required element with following dynamic content.
#for name:
#pipeline().parameters.name
#for id:
#pipeline().parameters.id
#for type:
#pipeline().parameters.type
#for prop_name:
#item().name
#for prop_type:
#item().type
The following is debug output of loop_activities (only for prop_name and prop_type) when I run pipeline1.

Logic App - Expression to get a particular substring from a json

I have out form one of the tasks in Logic App:
{
"headers": {
"Connection": "close",
"Content-Type": "application/json"
},
"body": {
"systemAlertId": "....",
"endTimeUtc": null,
"entities": [
{
"$id": "us_1",
"hostName": "...",
"azureID": "someID",
"type": "host"
},
{
"$id": "us_2",
"address": "fwdedwedwedwed",
"location": {
"countryCode": "",
},
"type": "ip"
},
],
}
}
I need initialize some variable named resourceID that contains value someID which is read from above example.
Value someID will always be found in the first member of Entities array, in that case I guess need to use function first
Any idea how expression of Initial variable should look?
Thanks
Considering the mentioned data you are receiving from Http trigger, I have used Parse JSON in order to get the inner values of the mentioned JSON. Here is how you can do it.
and now you can initialize the resourceID using 'Initialise variable' connector and set its value to azureID as per your requirement.
Have a look at the Parse JSON action.
To reference or access properties in JavaScript Object Notation (JSON) content, you can create user-friendly fields or tokens for those properties by using the Parse JSON action. That way, you can select those properties from the dynamic content list when you specify inputs for your logic app. For this action, you can either provide a JSON schema or generate a JSON schema from your sample JSON content or payload.
With the information in the JSON available in an object, you can more easily access it.

How do you check if any objects in an array has a property with value in Azure Logic App?

I am building a Azure Logic App which recieves a webhook. I need to parse the body and validate if a array contains any object with a property that matches either of two values, but have no idea how to do it.
Any ideas?
The JSON i am recieving in the webhook is like below, shortened for brevity. I am looking at the components-array:
{
"id": "12601",
"components": [
{
"id": "10021",
"name": "Value1"
},
{
"id": "10022",
"name": "Value2"
},
{
"id": "10023",
"name": "Value3"
}
]
}
From this array I need to output a parameter true/false if any properties with name == "Value1" or "Value" appears.
Is there any syntax similar to LINQ in C#, or do you use a foreach? I have not been able to find any simple way like:
If(Arr.Any(x => x.name == "Value1"))
Perhaps you can use Parse Json to parse the Json string you received, and then use condition to determine whether the name contains value1.
Please refer to my logic app:

Updating pipeline variables at a given scope using Azure DevOps REST api

I am currently trying to update a pipeline variable at the scope, DEV however, I am having hard time updating that variable. Is it possible to update the variable at a scope other than "Release"? If so, how? Below is the code that I used and the error that I received.
let reqLink = ' https://vsrm.dev.azure.com/'+ organization +'/'+project+'/_apis/release/releases?api-version=5.1';
let reqBody = {
"definitionId": definitionId,
"variables": {
"someVar":
{
"value": "foo",
"scope": "DEV"
}
}
};
sendHttpRequest('POST',reqLink,reqBody).then(response => {
let data = JSON.parse(response);
console.log(data);
});
This is the error that I am receiving:
{"$id":"1","innerException":null,"message":"Variable(s) someVar do not exist in the release pipeline at scope: Release
Scoped variables are defined not on the root level. But on stage level. So you must modify this here:
Here you have variable SomeVer scoped to Stage 1. The easiest way to achieve this will be hit endpoint with GET, manipulate on json and hit endpoint with PUT.
And what I noticed you are hiting release/releases whereas you should hit rather specific release release/releases/{releaseId}. Or maybe your goal is to update definition itself?
Is it possible to update the variable at a scope other than "Release"? If so, how?
The answer is yes.
The REST API you use is to create a release, if you want to update a release pipeline, using:
PUT https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/definitions?api-version=6.0-preview.4
The request body of the REST API may need detailed information of the release pipeline. Use the following REST API to get it.
GET https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/definitions/{definitionId}?api-version=6.0-preview.4
Then you can modify its response body and use it as the request body of the first REST API.
The property variables doesn't have a property called scope. If you want to update a variable from 'Release' scope to a stage scope, you need to delete the variable's original definition in variables and redefinite it in target environment. Here is an example.
Original script:
{
...
"variables": {
"somevar": {
"value": "foo"
}
},
...
};
The modified script:
{
...
"environments": [
{
"id": {stage id},
"name": DEV
...
"variables": {
"somevar": {
"value": "foo",
},
...
}
],
...
"variables": {},
...
};
Here is the summary: To change the scope of a variable, just move the variable definition to target scope.

Spark Dataframe: Generate an Array of Tuple from a Map type

My downstream source does not support a Map type and my source does and as such sends this. I need to convert this map into an array of struct (tuple).
Scala support Map.toArray which creates an array of tuple for you which seems like the function I need on the Map to transform:
{
"a" : {
"b": {
"key1" : "value1",
"key2" : "value2"
},
"b_" : {
"array": [
{
"key": "key1",
"value" : "value1"
},
{
"key": "key2",
"value" : "value2"
}
]
}
}
}
What is the most efficient way in Spark to do this assuming that also the field to change is a nested one. e.g
a is the root level dataframe column
a.b is the map at level 1 (comes from the source)
a.b_ is the array type of struct (this is what I want to generate in converting a.b to the array)
The answer so far goes some of the way I think, just can get the withColumn and UDF suggested to generate as below.
Thanks!
Just use an udf:
val toArray = udf((vs: Map[String, String]) => vs.toArray)
and adjust input type according to your needs.

Resources