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

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:

Related

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 to read json files with nested categories in node.js

I am using Perspective API (you can check out at: http://perspectiveapi.com/) for my discord application. I am sending an analyze request and api returning this:
{
"attributeScores": {
"TOXICITY": {
"spanScores": [
{
"begin": 0,
"end": 22,
"score": {
"value": 0.9345592,
"type": "PROBABILITY"
}
}
],
"summaryScore": {
"value": 0.9345592,
"type": "PROBABILITY"
}
}
},
"languages": [
"en"
],
"detectedLanguages": [
"en"
]
}
I need to get "value" in "summaryScore" as an integer. I searched it on Google, but i just found reading value for not categorized or only 1 times categorized json files. How can i do that?
Note: Sorry if i asked something really easy or if i slaughtered english. My primary language is not english and i am not much experienced on node.js
First you must make sure the object you have recived is presived by nodeJS as a JSON object, look at this answer for how first. After the object is stored as a JSON object you can do the following:
Reading from nested objects or arrays is as easy as doing this:
object.attributeScores.TOXICITY.summaryScore.value
If you look closer to the object and its structure you can see that the root object (the first {}) contains 3 values: "attributeScores", "languages" and "detectedLanguages".
The field you are looking for exists inside the "summeryScore" object that exists inside the "TOXICITY" object and so on. Thus you need to traverse the object structure until you get to the value you need.

JSONata - JSON to JSON Transformation in Nodejs API

I need to write REST API in Node jS for JSON to JSON transfromation.
There are many library and I sort listed "JSONata"
Please find JSONata simple sample here
The challenge is API receive JSON which has data and map but JSONata require map value without quotes.
{
"data" : {
"title" : "title1",
"description": "description1",
"blog": "This is a blog.",
"date": "11/4/2013"
},
"map" : {
"name": "title",
"info": "description",
"data" : {
"text": "blog",
"date": "date"
}
}
}
but the map object expected by JSONata is like below.
{
"name": title,
"info": description,
"some" : {
"text": blog,
"date": date
}
}
The above JSON key is in Quotes and value without Quotes.
Please find the NodeJS API code.
app.post('/JSONTransform', function(req, res, next)
{
const data = req.body.data;
const map = req.body.map;
var expression = jsonata(map);
var result = expression.evaluate(data);
res.send(result);
});
I can write simple function to remove quotes but the above map is simple example. It can be any no of child object and may have some special character in the value including quotes.
I prefer some npm library or standard way to remove quotes or configure JSONata to accepts quotes in value.
Appreciate if you suggest any other library or option.
This Node JS API is called from ASP.NET Core Web API.
ASP.NET Core Web API gets the data and map from database and pass this as single JSON to Node JS API.
Please suggestion solution to this problem or best alternative.
Thanks
Raj
I found solution to this problem.
Pass the single JSON that has both data and map. Since MAP is not valid JSON, I made entire map as string and escaped doubles quotes which is inside the string.
Please find the sample.
{
"map": "{ \"name\": title, \"info\": description, \"data\": { \"text\": blog, \"date\": date }}",
"data": {
"title": "title1",
"description": "description1",
"blog": "This is a blog.",
"date": "11/4/2013"
}
}

How to do field mapping in azure search for complex json objects for example nested array

I have following problem
I have a field mapping update to an index .Payload is complex where
I have:
{
"type": "abc",
"Party": [{
"Type": "abc",
"Id": "123",
"Name": "manasa",
"Phone": [{
"Type": "Office",
"Number": "12345"
}]
}]
}
And now I want to create a field for an index. The field name is phonenumber of type Collection(Edm.String)
where mapping is
{
"sourceFieldName" : "/Party/Phone/Number",
"targetFieldName" : "phonenumber",
"mappingFunction" : { "name" : "jsonArrayToStringCollection" }
}
In http post body
But still after indexing i get phone number result as null.That means the mapping went wrong.If you see the phone number in source json, it is inside a json array and it itself is an array and result needs to get stored inside a collection of a string.Is it possible how can I achieve this?
If this is not possible I atleast want field mapping till phone array ie., /Party/Phone/
If i index complete party array as a text, I get an error while running the index saying:
"Field 'partydetails' contains a term that is too large to process. The max length for UTF-8 encoded terms is 32766 bytes. The most likely cause of this error is that filtering, sorting, and/or faceting are enabled on this field, which causes the entire field value to be indexed as a single term. Please avoid the use of these options for large fields."
Can someone please help!
If party would have been a Json object than an array and phone would have been only a string array for example
{
"type": "abc",
"Party": {
"Type": "abc",
"Id": "123",
"Name": "manasa",
"Phone": [{
"12345",
"23463"
}]
}
}
Then I could have mapped
{
"sourceFieldName" : "Party/Phonenumber",
"targetFieldName" : "phonenumbers",
"mappingFunction" : { "name" : "jsonArrayToStringCollection" }
}
It map as collection of type odata EDM.string.
So to put this in better and straight forward way,
Either transform your json to something flatter (the example that I
gave above) or
Use the proper index incase if you know before inhand as
#Luis Cabrera said,
“sourceFieldName”: “/Party/0/Phone/0/Type
It is a limitation from azure search side.
Note that Party and Phone are arrays, so the field mapping you mention won't work.
You will need to index into the specific element. For example:
{
"sourceFieldName": "/Party/0/Phone/0/Type",
"targetFieldName": "firstPhoneNumberTypeOfFirstParty"
}
You may want to give that a shot.
Thanks!
Luis Cabrera | Program Manager | Azure Search

node.js and postgresql UPDATE nested JSON key

I have a table called "json" in my database, with 2 columns: "id" and "data"
Only one row is stored inside it at the moment, having 1 as id and a JSON structure as data:
{
"elements": {
"nodes": [
{
"data": {
"id": "n0",
"name": "Name here",
"color": "#FFFFFF"
}
},
{
"bob": "hello"
}
]
}
}
I need to update a key of the json: "Name here" has to become "updated"
This is what I tried:
db.query("UPDATE json SET $1 WHERE data->'elements'->'nodes'->0->'data'->'name'=$2", ['updated', 'Name here'])
but I get an error:
syntax error at or near "'updated'"
When using the Postgres JSON navigators it's important to terminate your chain with the text retrieval navigator ->> if you want to do comparisons like that:
UPDATE json SET $1 WHERE data->'elements'->'nodes'->0->'data'->>'name'=$2
That should permit comparing text to text instead of json.
I think you might also be able to use #>> to dig the whole way down in one shot:
UPDATE json SET $1 WHERE data#>>'{elements,nodes,0,data,name}'=$2

Resources