Logicapp Expression to read Dynamic Json path - read child element where parent path may change but hierarchy remaining same - expression

Hope all well.
I am in need of creating logicapp expression for reading child element in json where name of element & hierarchy remains same but parent name can be changing.
for example : JSON-1 :
{
"root": {
"abc1": {
"abc2": [
{
"element": "value1",
"element2": "value"
},
{
"element": "value2",
"element2": "valu2"
}
]
}
}
}
JSON-2 :
{
"root": {
"xyz1": {
"xyz2": [
{
"element": "value1",
"element2": "value"
},
{
"element": "value2",
"element2": "valu2"
}
]
}
}
}
I have tried these but no luck
approach-1: #{body('previous-action')?['']?['']?['element']
approach-2: #{body('previous-action')???['element']
Please let me know if anyone encountered this situation. Many thanks in advance.

I tend to find that converting the JSON to xml (at least in your case) is the simplest solution. Then when you've done that, you can't use XPath to simply make your selection.
Flow
In basic terms ...
I've defined a variable of type object that contains your JSON.
I then convert that JSON object to XML using this expression xml(variables('JSON Object'))
Next, initialize a variable is called Elements of type array (given you have multiple of them). The expression for setting that variable is where the smarts come in. That expression is ... xpath(xml(variables('XML')), '//element/text()') and it's getting the inner text of all element nodes in the XML.
Finally, loop through the results.
If you needed to take it up a level and get the second element then you'd need to change your xpath query to be a lot more generic so you can get the element2 nodes (and 3, 4, 5, etc. if they existed) in each array as well.
Note: I've stuck to your specific question of looking for element.
Result
This definition (which can be loaded directly into your tenant) demonstrates the thinking ...
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"For_Each_Element": {
"actions": {
"Set_Element": {
"inputs": {
"name": "Element",
"value": "#{item()}"
},
"runAfter": {},
"type": "SetVariable"
}
},
"foreach": "#variables('Elements')",
"runAfter": {
"Initialize_Element": [
"Succeeded"
]
},
"type": "Foreach"
},
"Initialize_Element": {
"inputs": {
"variables": [
{
"name": "Element",
"type": "string"
}
]
},
"runAfter": {
"Initialize_Elements": [
"Succeeded"
]
},
"type": "InitializeVariable"
},
"Initialize_Elements": {
"inputs": {
"variables": [
{
"name": "Elements",
"type": "array",
"value": "#xpath(xml(variables('XML')), '//element/text()')"
}
]
},
"runAfter": {
"Initialize_XML": [
"Succeeded"
]
},
"type": "InitializeVariable"
},
"Initialize_JSON_Object": {
"inputs": {
"variables": [
{
"name": "JSON Object",
"type": "object",
"value": {
"root": {
"abc1": {
"abc2": [
{
"element": "value1",
"element2": "value"
},
{
"element": "value2",
"element2": "valu2"
}
]
}
}
}
}
]
},
"runAfter": {},
"type": "InitializeVariable"
},
"Initialize_XML": {
"inputs": {
"variables": [
{
"name": "XML",
"type": "string",
"value": "#{xml(variables('JSON Object'))}"
}
]
},
"runAfter": {
"Initialize_JSON_Object": [
"Succeeded"
]
},
"type": "InitializeVariable"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {
"ParameterTest1": {
"defaultValue": "\"\"",
"type": "String"
}
},
"triggers": {
"manual": {
"inputs": {
"method": "GET",
"schema": {}
},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {}
}

Related

Extract value of array and add in the same select mongoDB

I am new to the mongoDB aggregation and I have this situation. I have this Json and I need to convert by "select" this object:
{
"type": "PF",
"code": 12345
"Name": Darth Vader,
"currency": "BRL",
"status": "SINGLE",
"adress": [
{
"localization": "DEATH STAR",
"createDate": 1627990848665
},
{
"localization": "TATOOINE",
"createDate": 1627990555665
},
]
}
this way:
{
"type": "PF",
"code": 12345
"Name": Darth Vader,
"currency": "BRL",
"status": "SINGLE",
"localization": "DEATH STAR",
"createDate": 1627990848665
},
{
"type": "PF",
"code": 12345
"Name": Darth Vader,
"currency": "BRL",
"status": "SINGLE",
"localization": "TATOOINE",
"createDate": 1627990555665
}
So, after my query is complete, I will have 02 objects instead of 01. How can I do this?
I would like to do this via select because after converting I will sort by createDate and limit the number of records to return to the API. I'm using Criteria em my project.
The way to do this is $unwind, this will make 1 copy of the document, for each member of the array.
Test code here
db.collection.aggregate([
{
"$unwind": {
"path": "$user.adress"
}
},
{
"$set": {
"user": {
"$mergeObjects": [
"$user",
"$user.adress"
]
}
}
},
{
"$unset": [
"user.adress"
]
},
{
"$sort": {
"createDate": 1
}
},
{
"$limit": 10
}
])
Edit1 (the above is if user is a field, if it was the name of the collection)
$$ROOT is a system variable that has as value all the document
Test code here
Query
db.collection.aggregate([
{
"$unwind": {
"path": "$adress"
}
},
{
"$replaceRoot": {
"newRoot": {
"$mergeObjects": [
"$$ROOT",
"$adress"
]
}
}
},
{
"$unset": [
"adress"
]
},
{
"$sort": {
"createDate": 1
}
},
{
"$limit": 10
}
])

Any idea how to do custom supportedCookingModes in Alexa discovery?

I'm trying to return a Discovery Response, but the supportedCookingModes only seems to accept standard values and only in the format of ["OFF","BAKE"], not Custom values as indicated by the documentation. Any idea on how to specify custom values?
{
"event": {
"header": {
"namespace": "Alexa.Discovery",
"name": "Discover.Response",
"payloadVersion": "3",
"messageId": "asdf"
},
"payload": {
"endpoints": [
{
"endpointId": "asdf",
"capabilities": [
{
"type": "AlexaInterface",
"interface": "Alexa.Cooking",
"version": "3",
"properties": {
"supported": [
{
"name": "cookingMode"
}
],
"proactivelyReported": true,
"retrievable": true,
"nonControllable": false
},
"configuration": {
"supportsRemoteStart": true,
"supportedCookingModes": [
{
"value": "OFF"
},
{
"value": "BAKE"
},
{
"value": "CUSTOM",
"customName": "FANCY_NANCY_MODE"
}
]
}
}
]
}
]
}
}
}
Custom cooking modes are brand specific. This functionality is not yet publicly available. I recommend you to choose one of the existing cooking modes:
https://developer.amazon.com/en-US/docs/alexa/device-apis/cooking-property-schemas.html#cooking-mode-values

Return only elements of an array in an object that contain a certain value

I've got the following document in an elastic search index:
{
"type": "foo",
"components": [{
"id": "1234123", ,
"data_collections": [{
"date_time": "2020-03-02T08:14:48+00:00",
"group": "1",
"group_description": "group1",
"measures": [{
"measure_name": "MEASURE_1",
"actual": "23.34"
}, {
"measure_name": "MEASURE_2",
"actual": "5"
}, {
"measure_name": "MEASURE_3",
"actual": "string_message"
}, {
"measure_name": "MEASURE_4",
"actual": "another_string"
}
]
},
{
"date_time": "2020-03-03T08:14:48+00:00",
"group": "2",
"group_description": "group2",
"measures": [{
"measure_name": "MEASURE_1",
"actual": "23.34"
}, {
"measure_name": "MEASURE_4",
"actual": "foo"
}, {
"measure_name": "MEASURE_5",
"actual": "bar"
}, {
"measure_name": "MEASURE_6",
"actual": "4"
}
]
}
]
}
]
}
Now I'm trying to figure out a mapping and a query for this document so the result would only contain the groups and measure_names I am interesed in. So far I'm able to query but I'll always retrieve the whole document which is not feasible since the array of measures can be quite large and most of the time I'd like a small subset.
For example I'm search for documents with "group": "1" and "measure_name": "MEASURE_" and the result I'd like to achieve looks like this:
{
"_id": "oiqwueou8931283u12",
"_source": {
"type": "foo",
"components": [{
"id": "1234123", ,
"data_collections": [{
"date_time": "2020-03-02T08:14:48+00:00",
"group": "1",
"group_description": "group1",
"measures": [{
"measure_name": "MEASURE_1",
"actual": "23.34"
}
]
}
]
}
]
}
}
I think what comes close to what I am looking for is the source parameter, but as far as I know there is no way to filter for values like {"measure_name": {"value": "MEASURE_1"}}
Thanks.
The simplest mapping that comes to mind is
PUT timo
{
"mappings": {
"properties": {
"components": {
"type": "nested",
"properties": {
"data_collections": {
"type": "nested",
"properties": {
"measures": {
"type": "nested"
}
}
}
}
}
}
}
}
and the search query would be
GET timo/_search
{
"_source": ["inner_hits", "type", "components.id"],
"query": {
"bool": {
"must": [
{
"nested": {
"path": "components.data_collections",
"query": {
"term": {
"components.data_collections.group.keyword": {
"value": "1"
}
}
},
"inner_hits": {}
}
},
{
"nested": {
"path": "components.data_collections.measures",
"query": {
"term": {
"components.data_collections.measures.measure_name.keyword": {
"value": "MEASURE_1"
}
}
},
"inner_hits": {}
}
}
]
}
}
}
Notice the inner_hits param under each subquery and that the _source param is limited so that we don't return the whole hit, but rather only the subgroups that did match. type and component.id cannot be "seen" in the nested fields so we've included them explicitly.
The response should then look like this:
You now have precisely the attributes you need so a bit of post-processing will get you the desired format!
I'm not familiar w/ a cleaner way of doing this but if any of y'all do, I'd be glad to learn it.

How to force object key name in array

I am using YAML to mark up some formulas and using JSON schema to provide a reference schema.
An example of the YAML might be:
formula: # equates to '5 + (3 - 2)'
add:
- 5
- subtract: [3, 2]
While I have figured out how to make the immediate child object of the formula ("add" in this example) have the right key name and type (using a "oneOf"array of "required"s). I am not sure how to ensure that object of an array ("subtract") likewise use specific key names.
So far, I can ensure the type using the following. But with this method, as long as the object used matches the subtract type, it is allowed any key name, it is not restricted to subtract:
"definitions: {
"add": {
"type": "array",
"minItems": 2,
"items": {
"anyOf": [
{ "$ref": "#/definitions/value"}, # value type is an integer which allows for the shown scalar array elements
{ "$ref": "#/definitions/subtract" }
// other operation types
]
}
},
"subtract": {
"type": "array",
"minItems": 2,
"maxItems": 2,
"items": {
"anyOf": [
{ "$ref": "#/definitions/value"},
{ "$ref": "#/definitions/add" }
// other operation types
]
}
}
// other operation types
}
How can I introduce a restriction such that the keys of objects in the array match specific names, while still also allowing scalar elements?
It sounds like what you want is recursive references.
By creating a new definition which is oneOf the operations and value, which then allow items which then reference back to the new definition, you have recursive references.
"definitions: {
"add": {
"type": "array",
"minItems": 2,
"items": { "$ref": "#/definitions/operations_or_values"},
},
"subtract": {
"type": "array",
"minItems": 2,
"maxItems": 2,
"items": { "$ref": "#/definitions/operations_or_values"},
}
// other operation types
"operations_or_values": {
"anyOf": [
{ "$ref": "#definitions/add" },
{ "$ref": "#definitions/subtract" },
{ "$ref": "#definitions/value" }, # value type is an integer which allows for the shown scalar array elements
{ "$ref": "#definitions/[OTHERS]" },
]
}
}
I haven't had time to test this, but I believe it will be, or be close to, what you need. Let me know if it doesn't work. I may not have full understood the question.
What a fascinating problem! This remarkably concise schema can express any expression.
{
"type": ["object", "number"],
"propertyNames": { "enum": ["add", "subtract", "multiply", "divide"] },
"patternProperties": {
".*": {
"type": "array",
"minItems": 2,
"items": { "$ref": "#" }
}
}
}
So what I ended up doing was extending the idea of I already used with the '"oneOf"array of "required", adding an "anyOf".
Thus, an operator schema is now:
"definitions": {
"add": {
"type": "array",
"minItems": 2,
"items": {
"anyOf": [
{ "$ref": "#/definitions/literal" }, // equates to a simple YAML scalar
{ "$ref": "#/definitions/constant" },
{ "$ref": "#/definitions/variable" },
{
"type": "object",
"oneOf": [
{ "required": ["add"] },
{ "required": ["subtract"] }
// more operator names
],
"properties": {
"add": { "$ref": "#/definitions/add" },
"subtract": { "$ref": "#/definitions/subtract" }
// more operator type references
}
}
]
}
},
// more definitions
}
This can be refactored to something that applies more easily across different operators like so:
"definitions": {
"operands": {
"literal": { "type": "number" }, // equates to a simple YAML scalar
"constant": {
"type": "object",
"properties": {
"value": { "type": "number" }
},
"required": [ "value" ]
},
"variable": {
"type": "object",
"properties": {
"name": { type": "string" },
"type": { "type": "string" }
},
"required": [ "name", "type" ]
}
}
"operators": {
"add": {
"type": "array",
"minItems": 2,
"items": { "$ref": "#/definitions/anyOperandsOrOperators" }
},
"subtract": {
"type": "array",
"minItems": 2,
"maxItems": 2,
"items": { "$ref": "#/definitions/anyOperandsOrOperators" }
}
// more operator types
},
"anyOperator": {
"type": "object",
"oneOf": [
{ "required": ["add"] },
{ "required": ["subtract"] }
// more operator names
],
"properties": {
"add": { "$ref": "#/definitions/operators/add" },
"subtract": { "$ref": "#/definitions/operators/subtract" }
// more operator type references
}
},
"anyOperandsOrOperators":
{
"anyOf": [
{ "$ref": "#/definitions/operands/literal" },
{ "$ref": "#/definitions/operands/constant" },
{ "$ref": "#/definitions/operands/variable" },
{ "$ref": "#/definitions/anyOperator"}
]
}
}
And this means the YAML for an operator can look as follows
\/mapping \/mapping
add:[ 5, subtract:[ *constantA, *variableB ] ]
scalar^ ^mapping with specific name

Multiple searches with with json_query/jmespath filter in Ansible

I'm trying to parse out specifc subnet names in the following piece of json, while using contains_with or starts_with filters in json_query.
It contains two vnets each of which has multiple subnets:
{
"azure_virtualnetworks": [
{
"name": "test-vnet-172-17-0-0-19",
"properties": {
"subnets": [
{
"name": "test-confluent-subnet-172-17-0-0-28",
"properties": {
"addressPrefix": "172.20.88.0/28",
"networkSecurityGroup": {
"id": "/subscriptions/********/resourceGroups/test-confluent-rg/providers/Microsoft.Network/networkSecurityGroups/test-confluent-nsg"
},
"provisioningState": "Succeeded"
}
},
{
"name": "test-test-subnet-172-17-0-32-28",
"properties": {
"addressPrefix": "172.20.88.32/28",
"networkSecurityGroup": {
"id": "/subscriptions/********/resourceGroups/test-test-rg/providers/Microsoft.Network/networkSecurityGroups/test-test-nsg"
},
"provisioningState": "Succeeded"
}
}
]
}
},
{
"name": "test2-vnet-172-17-1-0-19",
"properties": {
"subnets": [
{
"name": "test-confluent-subnet-172-17-1-0-28",
"properties": {
"addressPrefix": "172.20.88.0/28",
"networkSecurityGroup": {
"id": "/subscriptions/********/resourceGroups/test-confluent-rg/providers/Microsoft.Network/networkSecurityGroups/test-confluent-nsg"
},
"provisioningState": "Succeeded"
}
},
{
"name": "test-qatesting-subnet-172-17-1-16-28",
"properties": {
"addressPrefix": "172.20.88.16/28",
"networkSecurityGroup": {
"id": "/subscriptions/********/resourceGroups/test-qatesting-rg/providers/Microsoft.Network/networkSecurityGroups/test-qatesting-nsg"
},
"provisioningState": "Succeeded"
}
}
]
}
}
]
}
I need to search for a subnet name after searching by virtual network name.
I can filter as far down as the list of subnets without problems. e.g
azure_virtualnetworks[?contains(name,`test2-vnet`)].properties.subnets[]
returns:
[
{
"name": "test-confluent-subnet-172-17-1-0-28",
"properties": {
"addressPrefix": "172.20.88.0/28",
"networkSecurityGroup": {
"id": "/subscriptions/********/resourceGroups/test-confluent-rg/providers/Microsoft.Network/networkSecurityGroups/test-confluent-nsg"
},
"provisioningState": "Succeeded"
}
},
{
"name": "test-qatesting-subnet-172-17-1-16-28",
"properties": {
"addressPrefix": "172.20.88.16/28",
"networkSecurityGroup": {
"id": "/subscriptions/********/resourceGroups/test-qatesting-rg/providers/Microsoft.Network/networkSecurityGroups/test-qatesting-nsg"
},
"provisioningState": "Succeeded"
}
}
]
However I'm having problems then searching the subnets. I had thought that some variation on following might work but haven't had any sucess:
azure_virtualnetworks[?contains(name,`test2-vnet`)].properties.subnets[?contains(name,`test-confluent`) ]
I'm struggling to figure out what the correcting syntax is here.
Select required subnets, stop projection with pipe expression, filter required items from the subnets list:
azure_virtualnetworks[?contains(name,`test2-vnet`)].properties.subnets[] | [?contains(name,`test-confluent`)]

Resources