How to handle null or Non required column in json in Azure Logic App - azure-logic-apps

sample api url: https://jsonplaceholder.typicode.com/posts
title is not a mandatory field in received json. It may or may not be part of each record.
When this field is missing in record, #{items('For_each')['title']} this throws an exception.
I want the value of myVariable to set as 'N/A' in that case. How do i do this?

I make the assumption that this is an array and that you have a set schema in the HTTP trigger. If the schema is set, make sure you remove Title as a required field.
Using these asumptions you should be able to do the following with Coalesce()
If Title now is not present in the body of the HTTP request the Title will be equal to 'N/A'
Using postman to test, note, the result is backward as it is the first object sent in my array.

Cause you url data all have the title, so I test with When a HTTP request is recived trigger to get the json data.
In this situation the data could only be like this:
{
"userId": 1,
"id": 2,
"body": "xxxxx"
}
not like the below one:
{
"userId": 1,
"id": 2,
"title":,
"body": "xxxxxxxxx"
}
I test with the first one, it did show the error message: property 'title' doesn't exist,. So here is my solution, after the action Set variable, add an action to set the variable you want and set the action run after Set variable has failed like the below picture.
After configuration, if the title doesn't exist, it will be set as N/A as you want.
Hope this could help you, if this is not what you want or you have other questions, please let me know.

Related

Logic Apps: Data not parsed on the second query inside "foreach" loop

Hi Logic Apps Experts,
I'd like to check with you some of the foreach loop behaviors, and to check whether this is expected/ is there any workarounds for this.
So the steps with this logicapps is to "Run query and list results" search will do is searching SecurityIncident table. And foreach SecurityIncident record, find a corresponding SecurityAlert record in "Using IncidentId-Query Details of the Alert" step.
For the first query, the data is parsed properly and each fields can be used.
However, after the second query I can only use 'Body' and 'value' in the steps. Which contains unparsed values.
Questions:
Is this behavior expected?
Is there a better way to ensure the second query is parsed?
Any other room of improvements advice are greatly appreciated.
Thank you!
The selection list affected by the required type/format of the input box in the action. So I think the behavior is expected.
If you want to get the parsed field from the query action, you can use expression. I'm not clear about the details of query result body, here I just provide a sample for your reference:
For example, if the query result shows like:
{
"body": [
{
"TenantId": "111",
"xxxx": "xxx"
},
{
"TenantId": "222",
"xxxx": "xxx"
}
]
}
Then you can use the expression body('Run_query_and_list_results')[0]?['TenantId'] to get the value of first TenantId. In a word, use [index] to get array, use ?['key'] to get map.

How to use Secure String Parameters in Logic Apps

I don't understand the purpose of the greyed out Actual Value field in the Logic Apps Designer. I guess it might be relevant when dealing with a Secure String, especially since this message comes up when entering a value in Default Value for this type of Parameter:
It is not recommended to set a default value for type 'SecureString'
because it will be stored as plain text.
I imagine there might be a way to pass the actual ApiKey value via a different mechanism at runtime, which would supersede my default string "secret", but I have no idea how to do this. For instance, if my Logic App is on an HTTP Trigger, would passing a header? or query param? called "ApiKey" with the actual key work?
Where is this process documented for all Trigger types?
And still, what is the purpose of the Actual Value field in Designer?
Edit following #hury-shen's comment dated 2020-10-29 below:
This is what I've tried:
however this (still) returns:
Operation failed: The request content is not valid and could not be
deserialized: 'Could not find member 'reference' on object of type
'FlowTemplateParameter'. Path
'properties.parameters.ApiKey.reference', line 1, position 3710.'.
when I try to switch back to Designer mode.
When we define parameters in logic app, the value will be visible in the code view of the logic app although we choose "Secure String" type. If you want to use username, password and secret as parameters, you can store them in azure key vault and then define the parameters at the workflow definition level like this:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
// Template parameter values
"parameters": {
"<parameter-name-1>": {
"value": "<parameter-value>"
},
"<parameter-name-2>": {
"value": "<parameter-value>"
},
"<secured-parameter-name>": {
"reference": {
"keyVault": {
"id": "/subscriptions/<Azure-subscription-ID>/resourceGroups/<Azure-resource-group-name>/Microsoft.KeyVault/vaults/<key-vault-name>",
},
"secretName: "<secret-name>"
}
},
<other-parameter-values>
}
}

HTTP Request logic app - parse values from array name, value pair

I am POSTing valid JSON to a logic app and the request body is a simple set of name/value pairs in JSON format. I use parse JSON action to turn the JSON into a variable. (I am not sure if this is absolutely required, or if I can reference the http body directly without a JSON object).
[
{
"name": "fullname",
"value": "joe schmoe"
},
{
"name": "email",
"value": "joeschmoe#acme.com"
}
]
All I need to do (and this is driving me nuts) is create two variables, one containing the value of the email field and one containing the value of the fullname field.
As soon as I try to use the output value value, the logic app replaces my action with a For Each action, and then I try to to assign item().value[0] or item().value[1] to a variable without luck.
I have read dozens of examples online, but of course they all seem to be parsing JSON where there is largely unique elements in the name:value pairs.
While this is a bit of a newb question, I'm confused and need advice.
Thank you.
I used a parseJSON action to ensure I have a varaible containing the JSON array.
I then referenced the array value with (explained):
"from the body of the output from the Parse JSON action, refernce the first record in the set (fullname) and then the value of that record, the value 'joe schmoe'"
#{body('Parse_JSON')[0]['value']} (returns fullname)
Email is similar, just 2nd record in the collection:
#{body('Parse_JSON')[1]['value']} (returns email address)

Adding a field mapping to 'base64Encode' an index field by calling REST endpoint not working: "A resource without a type name was found"

I'm trying to update a search index (using the Update Indexer) by sending a PUT request to https://searchservicename.search.windows.net/indexes/indexName?api-version=2017-11-11 and it's not working.
If I make the exact same request with the list of fields, the request works as expected and I receive a 200. As soon as I try to add fieldMappings as well, I get an error.
My Json I'm adding to the request as "application/json":
{
"name": "indexName",
"fields": [
<List of Valid Fields w/ Valid Types>
],
"fieldMappings": [
{
"sourceFieldName": "fieldName",
"targetFieldName": "fieldName",
"mappingFunction": {
"name": "base64Encode"
}
}
]
}
When calling the API, the error I'm getting is:
{Search request failed: {"error":{"code":"","message":"The request is invalid. Details: index : A resource without a type name was found, but no expected type was specified. To allow entries without type information, the expected type must also be specified when the model is specified.\r\n"}}
I expect the request to return 200 and have the field mapping added.
The error I get seems to be related to the list of fields but as mentioned before, the request works as expected with the same body minus the field mappings.
Let me know if you need any other information from me - Thanks.
Field mappings should be added to an indexer, not an index. Based on your request, you are trying to update an index.

How to fetch data from couchDB using couch api?

Instead keys and IDs alone, I want to get all the docs via couch api. I have tried with GET "http://localhost:5984/db-name/_all_docs" but it returned
{
"total_rows":4,
"offset":0,
"rows":[
{"id":"11","key":"11","value":{"rev":"1-a0206631250822b37640085c490a1b9f"}},
{"id":"18","key":"18","value":{"rev":"30-f0798ed72ceb3db86501c69ed4efa39b"}},
{"id":"3","key":"3","value":{"rev":"15-0dcb22bab2b640b4dc0b19e07c945f39"}},
{"id":"6","key":"6","value":{"rev":"4-d76008cc44109bd31dd32d26ba03125d"}}
]
}
From the documentation
for the below request it will send the data as we expected but it requires set of keys in request.
POST /db/_all_docs HTTP/1.1
{
"keys" : [
"11",
"18"
]
}
Thanks in advance.
The _all_docs endpoint is actually just a system-level view that uses the _id field as the index. Thus, any parameters that you can use for views also apply here.
If you read the documentation further, you'll find that adding the parameter include_docs=true to your view will include the original documents in the results. The documents will be added as the doc field alongside id, value and rev.

Resources