Handlebar-helpers built-in blocks. Complex sum - handlebars.js

Without creating a new helper and just use the existing one's from "handlebar-helpers"
So I am actually trying to add the values in the below specified payload.
{
"products": [
{
"product": [
{
"price": 10
},
{
"price": 20
},
{
"price": 30
}
]
},
{
"product": [
{
"price": 10
},
{
"price": 20
},
{
"price": 30
}
]
}
]
}
From the above payload. I am trying to compute the sum of products array based upon the index position of product array.
The output for this payload is 20 , 40 ,60 respectively.
I tried with pluck block as,
{{pluck products "products.price"}} but it simply didn't work.
Is there any workaround for this ?
Current logic that doesn't works.
{{#each products}}
{{#each product}}
{{sum (pluck products "product.price")}}
{{/each}}
{{/each}}

Related

Map to an array within an object within an array

I am still having trouble understanding how to use the map function. In this case my payload is a JSON object that contains an array of "orders" with each "order" being an object... How do I create a map that would let me get to the array of "ContactEmailAddresses"?
{
"orders": [
{
"OrderGroupNumber": 1,
"Requester": {
"Name": "Mickey Mouse"
},
"ContactEmailAddresses": [
"user1#abc.com",
"user2#abc.com"
],
"CreatedByEmailAddress": "user1#abc.com"
},
{
"OrderGroupNumber": 2,
"Requester": {
"Name": "Donald Duck"
},
"ContactEmailAddresses": [
"user3#abc.com",
"user4#abc.com"
],
"CreatedByEmailAddress": "user3#abc.com"
},
{
"OrderGroupNumber": 3,
"Requester": {
"Name": "Goofy"
},
"ContactEmailAddresses": [
"user5#abc.com",
"user6#abc.com"
]
}
]
}
My current attempt that doesn't work is:
payload.*orders map (order, index) ->
{
order.contactEmailAddresses
}
%dw 2.0
output application/json
---
payload.orders flatMap $.ContactEmailAddresses
Output:
[
"user1#abc.com",
"user2#abc.com",
"user3#abc.com",
"user4#abc.com",
"user5#abc.com",
"user6#abc.com"
]

Hasura - query for tags - null array supposed to return all results, but only returns items with tags

I have a query:
query SearchProductData($tagId: [Int!]) {
product(where: { productTags: {_or: {tagId: {_in: $tagId}}}}) {
id
name
productTags {
tag {
id
name
}
}
}
}
and I pass in a variable of
{"tagId": null}
I would like to get back all product, regardless of if they have tags applied or not. What happens though is it retrieves only items with tags applied, not including the items with no tags.
the DB schema is
|- product
|
|-- productTags (one to many linking table of productIDs and tagIDs)
|
|--- tags
Any ideas how to write a query for this use case?
This is expected because of how the where clause is translating to a join (you can see the Generated SQL and Execution Plan query analysis by clicking Analyze in GraphiQL console.)
I don't think it's possible without injecting a bool expression. See below:
E.g.
query Test($expression: product_bool_exp) {
product(where: $expression) {
id
name
product_tags {
tag {
id
name
}
}
}
}
with argument
{
"expression": {"product_tags": {"tag_id": {"_in": [2]}}}
}
returns back:
{
"data": {
"product": [
{
"id": 1,
"name": "product_A",
"product_tags": [
{
"tag": {
"id": 1,
"name": "tag_1"
}
},
{
"tag": {
"id": 2,
"name": "tag_2"
}
}
]
}
]
}
}
And for the other case using the same query (where there are no tags passed in, and therefore all products we can use this variable value:
{
"expression": null
}
And we get back...
{
"data": {
"product": [
{
"id": 1,
"name": "product_A",
"product_tags": [
{
"tag": {
"id": 1,
"name": "tag_1"
}
},
{
"tag": {
"id": 2,
"name": "tag_2"
}
}
]
},
{
"id": 4,
"name": "product_D",
"product_tags": []
}
]
}
}
So you can dynamically construct that where expressions and pass that as an argument to the query.

CosmosDB, help flatten and filter by nested array

I'm trying to flatten and filter my json data that is in a CosmosDB.
The data looks like below and I would like to flatten everything in the array Variables and then filter by specific _id and Timestamp inside of the array:
{
"_id": 21032,
"FirstConnected": {
"$date": 1522835868346
},
"LastUpdated": {
"$date": 1523360279908
},
"Variables": [
{
"_id": 99999,
"Values": [
{
"Timestamp": {
"$date": 1522835868347
},
"Value": 1
}
]
},
{
"_id": 99998,
"Values": [
{
"Timestamp": {
"$date": 1523270312001
},
"Value": 8888
}
]
}
]
}
If you want to flatten data from the Variables array with properties from the root object you can query your collection like this:
SELECT root._id, root.FirstConnected, root.LastUpdated, var.Values
FROM root
JOIN var IN root.Variables
WHERE var._id = 99998
This will result into:
[
{
"_id": 21032,
"FirstConnected": {
"$date": 1522835868346
},
"LastUpdated": {
"$date": 1523360279908
},
"Values": [
{
"Timestamp": {
"$date": 1523270312001
},
"Value": 8888
}
]
}
]
If you want to even flatten the Values array you will need to write something like this:
SELECT root._id, root.FirstConnected, root.LastUpdated,
var.Values[0].Timestamp, var.Values[0]["Value"]
FROM root
JOIN var IN root.Variables
WHERE var._id = 99998
Note that CosmosDB considers "Value" as a reserved keyword and you need to use an escpape syntax. The result for this query is:
[
{
"_id": 21032,
"FirstConnected": {
"$date": 1522835868346
},
"LastUpdated": {
"$date": 1523360279908
},
"Timestamp": "1970-01-01T00:00:00Z",
"Value": 8888
}
]
Check for more details https://learn.microsoft.com/en-us/azure/cosmos-db/sql-api-sql-query#Advanced
If you're only looking for filtering by the nested '_id' property then you could use ARRAY_CONTAINS w/ the partial_match argument set to true. The query would look something like this:
SELECT VALUE c
FROM c
WHERE ARRAY_CONTAINS(c.Variables, {_id: 99998}, true)
If you also want to flatten the array, then you could use JOIN
SELECT VALUE v
FROM v IN c.Variables
WHERE v._id = 99998

return all entity values from watson conversation dialog

I'm working with Watson Conversation, inside a dialog.
I want to return all values of one entity in an array context variables.
The following works
{
"context": {
"toppings_array":["#toppings[0]","#toppings[1]"] works.
...
}
I'd like to find a generic solution, such as
{
"context": {
"toppings_array":["#toppings"]
...
}
The above sample sets the toppings_array value to the first element of the entity (e.g. #toppings[0]).
Thx.
You can do the following:
{
"context": {
"toppings_array": "<? entities['toppings'] ?>"
...
}
You would end up with something like:
[
{
"entity": "toppings",
"location": [
4,
13
],
"value": "pepperoni",
"confidence": 1
},
{
"entity": "toppings",
"location": [
14,
23
],
"value": "sprinkles",
"confidence": 1
}
]
Just Use # Topping.values . It will return all the values in form of array in context variable.

Processing conditions based on context variables in Watson Conversation Service

Is it possible to set the value of a context variable based on a set of conditions which depend on value of another context value? I am trying to integrate NLU(Natural Language Understanding) service with Conversation service and would like to set some context variables based on the values returned by NLU. For example I am getting following entities from NLU:
{
...
"context": {
...
"nlu_response": {
"entities": [{
"type": "Person",
"relevance": 0.5,
"count": 1,
"text": "Jon Doe",
"emotion": {
"anger": 0.082378,
"disgust": 0.033499,
"fear": 0.072588,
"joy": 0.100971,
"sadness": 0.147584
},
"sentiment": {
"score": 0.409803
}
},
{
"type": "Person",
"relevance": 0.5,
"count": 1,
"text": "Jane Doe",
"emotion": {
"anger": 0.140151,
"disgust": 0.091598,
"fear": 0.059244,
"joy": 0.046762,
"sadness": 0.165763
},
"sentiment": {
"score": 0
}
}]
}
}
}
and would like to create a context variable EntityPerson_1 with a value of "Jon Doe" only if there are values in entity object with type="Person".
In other words is something like this possible in a response node:
{
...
"context": {
...
"EntityPerson_1": <? context.nlu_response.entities.size()>0 && context.nlu_response.entities.get(0).type.contains('Person')?context.nlu_response.entities.get(0).text:'' ?>
}
}
Yes, it is possible. Your code is almost correct. Working code is:
{
...
"context": {
...
"EntityPerson_1": "<? context.nlu_response.entities.size()>0 && context.nlu_response.entities.get(0).type.contains('Person')?context.nlu_response.entities.get(0).text:'' ?>"
}

Resources