Logic App - change JSON format using native actions only - azure-logic-apps

I have a below JSON input coming from a source system:
{
"d":{
"results":[
{
"userId":"123",
"employmentType":"Full time",
"employment":{
"compansation":{
"results":[
{
"payments":{
"results":[
{
"payType":"Annual Salary",
"value":"70000"
},
{
"payType":"Annual Leave",
"value":"1000"
},
{
"payType":"Other Payments",
"value":"2000"
}
]
}
}
]
}
}
},
{
"userId":"456",
"employmentType":"Full time",
"employment":{
"compansation":{
"results":[
{
"payments":{
"results":[
{
"payType":"Annual Salary",
"value":"80000"
},
{
"payType":"Annual Leave",
"value":"2000"
},
{
"payType":"Other Payments",
"value":"3000"
}
]
}
}
]
}
}
},
{
"userId":"123",
"employmentType":"Full time",
"employment":{
"compansation":{
"results":[
{
"payments":{
"results":[
{
"payType":"Annual Salary",
"value":"90000"
},
{
"payType":"Annual Leave",
"value":"3000"
},
{
"payType":"Other Payments",
"value":"4000"
}
]
}
}
]
}
}
}
]
}
}
I want to filter "employment/compansation/payments" to use "payType" "Annual Salay" and "Annual Leave" only; and filter out "payType: Other Payments". Then sum both "Annual Salary" and "Annual Leave" and generate final output like:
[
{
"userId":"123",
"employmentType":"Full time",
"totalSalary":"71000"
},
{
"userId":"456",
"employmentType":"Full time",
"totalSalary":"82000"
},
{
"userId":"789",
"employmentType":"Full time",
"totalSalary":"93000"
}
]
Can I achieve this by only using native Logic App actions? Without using Functions or even JavaScript code? And how?

You can use the "Filter Array" connector in Logic App.
This might be a duplicate question, with similar question being answered here.

Related

How to delete all sub array

In my MongoDB database, I have a collection 'produits' with documents like this
{
"_id": {
"$oid": "6048e97b4a5f000096007505"
},
"modeles": [
{
"id": "OppoA3",
"pieces": [
{
"id": "OppoA3avn"
},
{
"id": "OppoA3bat"
}]
]
},
{
"id": "OppoA1",
"pieces": [
{
"id": "OppoA1avn",
},
{
"id": "OppoA1batt",
}
]
}
]
}
How can I delete all modeles.pieces from all my documents.
I managed to delete with a filter on modeles.id but with that code but not on all the collection
db.produits.update(
{marque_id:'OPPO', 'modeles.id':'RENOZ'},
{$set:
{
'modeles.$.pieces': []
}
}
, { multi : true }
)
I would like all documents like this finally
{
"_id": {
"$oid": "6048e97b4a5f000096007505"
},
"modeles": [
{
"id": "OppoA3",
"pieces": []
},
{
"id": "OppoA1",
"pieces": []
}
]
}
Thank you for your help.
I have done a javascript loop like this, but i think it's not best practice
async removePieces(){
var doc
try {
doc = await produitModel.find()
for (var produit of doc) {
for (var modele of produit.modeles) {
const filter = {'marque_id': produit.marque_id, 'modeles.id': modele.id}
const set = {
$set: {
'modeles.$.pieces': []
}
}
await db.collection('produits').updateOne(filter, set)
}
}
console.log('removePieces() ==> Terminé')
} catch(err) {
console.log(err)
}
}
db.produits.update({
modeles: {//This is because your second document will create failure otherwise
$exists: true
}
},
{
$set: {
"modeles.$.pieces": []
}
},
{
multi: true
})

MongoDB: update from ObjectId to string for many documents

I have a complex structure similar to:
{
type: "Data",
data: [
"item1": {...},
"item2": {...},
...
"itemN": {
"otherStructure": {
"testData": [
{
"values": {...}
},
{
"values": {
"importantKey": ObjectId("23a2345gf651")
}
}
]
}
}
]
}
For such kind of data structure I want to update data type for all these importantKeys from ObjectId into string.
I was trying queries similar to:
db.collection.updateMany(
{type:"Data"},
{$toString: "data.$[element].otherStructure.testData.$[element].values.importantKey"},
{"data.element.otherStructure.testData.element.values.importantKey": {$type: "objectId"}})
But all these tries were no successful.
So, are there any adequate solutions for updating such data?
UPDATE
Sorry for confusing, my structure is more complex:
data.content.$[element].otherStructure.testData.keys.$[element].values.$[element].meta.importantKey
All these $[element] elements means objects with list of elements.
You may use this workaround:
db.collection.aggregate([
{
$addFields: {
"data.content": {
$map: {
input: "$data.content",
as: "data",
in: {
otherStructure: {
testData: {
keys: {
$map: {
input: "$$data.otherStructure.testData.keys",
as: "testData",
in: {
"values": {
$map: {
input: "$$testData.values",
as: "values",
in: {
"meta": {
"importantObject": {
"importantKey": {
$toString: "$$values.meta.importantObject.importantKey"
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
},
{$out:"collection"}
])
MongoPlayground

Fixing a Simple Elasticsearch Query

I have below data:
{
"results":[
{
"ID":"1",
"products":[
{
"product":"car",
"number":"5"
},
{
"product":"computer",
"number":"212"
}
]
},
{
"ID":"2",
"products":[
{
"product":"car",
"number":"9"
},
{
"product":"computer",
"number":"463"
},
{
"product":"bicycle",
"number":"5"
}
]
}
]
}
And my query is below:
{
"query":{
"bool":{
"must":[
{
"wildcard":{
"results.products.product":"*car*"
}
},
{
"wildcard":{
"results.products.number":"*5*"
}
}
]
}
}
}
What I expect is to get only ID1. Because only it has a product with { "product":"car", "number":"5" } record. But what I get is both ID1 and ID2 because ID2's first record has "product":"car" and third record has "number":"5" records separately.
How can I fix this query?
You need to define your products as a nested type when creating mapping. Try with following mapping example:
PUT http://localhost:9200/indexname
{
"mappings": {
"typename": {
"properties": {
"products" : {
"type" : "nested"
}
}
}
}
}
Then you can use nested queries to match entire elements of your array - just as you need to.
{
"query": {
"nested": {
"path": "products",
"query": {
"bool": {
"must": [
{ "wildcard": { "products.product": "*car*" }},
{ "wildcard": { "products.number": "*5*" }}
]
}
}
}
}
}

Build json builder with arrayJson in groovy

I am new in groovy and I want to construct a json object with the builder
{
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{ "match": { "content": "scontent" } },
{ "match": { "title":"stitle" } }
]
}
},
{
"bool": {
"should": [
{ "match": { "a1": "v1" } },
{ "match": { "a2":"v2" } },
... and so on ...
{ "match": { "an":"vn" } }
]
}
}
]
}
},
"highlight": {
"fields": {
"content":{}
}
}
}
I search a lot of on other posts on stackoverflow and I write this code
So I did this but no way to get what I want :
JsonBuilder builder = new JsonBuilder()
def body = builder {
from Lib.or(qQuery.start, 0)
size Lib.or(qQuery.num, 10)
query {
bool {
must [
{
bool {
should [
{ match { content 'scontent' } },
{ match { title 'stitle' } }
]
}
},
{
bool {
should myVals.collect {[
'match' : { it.key it.value }
]}
}
}
]
}
}
highlight {
fields {
content {}
}
}
}
Thanks for any help !
I think you can make this work with the JsonBuilder as is, but it is usually easier to create the data structure using maps and lists (which is what the builder outputs) in groovy as you have more control there.
Example code:
import groovy.json.*
def data = [
query: [
bool: [
must: [
[bool:
[should: [
[match: [ content: 'scontent']],
[match: [ title: 'stitle']]
]]
],
[bool:
[should: [
[match: [ a1: 'v1']],
[match: [ a2: 'v2']],
[match: [ vn: 'vn']]
]]
]
]
]
]
]
println JsonOutput.prettyPrint(JsonOutput.toJson(data))
produces:
{
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"match": {
"content": "scontent"
}
},
{
"match": {
"title": "stitle"
}
}
]
}
},
{
"bool": {
"should": [
{
"match": {
"a1": "v1"
}
},
{
"match": {
"a2": "v2"
}
},
{
"match": {
"vn": "vn"
}
}
]
}
}
]
}
}
}
I did not include your full json as it takes up some space, but the structure is there. Note the use of lists ([valueA, valueB]) vs maps ([someKey: someValue]) in the data structure.
Granted this makes the JsonBuilder less than 100% useful but I haven't seen any concise ways of including lists of large json objects in a list within the structure. You can do:
def json = JsonBuilder()
json.query {
bool('list', 'of', 'values')
}
but for larger structures as list elements I would say go with the lists and maps approach.

How access the nested json data using ng-repeat or angular.forEach

i have the following json file. i am unable to get the data dynamically. please help me to solve this issue.
my requirement is i need to view the url value based on the submodule ie; sname || id. if i am accessing the sname as chapter1 i need to get all the vid -> url values that are present for that sub module.
[
{
"name":"Introduction - About The PMP Credential",
"sub1":[
{
"sname":"Introduction to PMP",
"id":"1",
"vid":[
{
"url":"www.abcd.co"
},
{
"url":"www.xyz.com"
},
{
"url":"www.xyz.com"
}
]
},
{
"sname":"Chapter1",
"id":"2",
"vid":[
{
"url":"www.abcd.co"
},
{
"url":"www.xyz.com"
}
]
},
{
"sname":"Justification",
"id":"3",
"vid":[
{
"url":"www.abcd.co"
},
{
"url":"www.xyz.com"
}
]
}
]
},
{
"name":"Project Management Framework",
"sub1":[
{
"sname":"Project Seminar",
"id":"4",
"vid":[
{
"url":"www.abcd.co"
},
{
"url":"www.xyz.com"
}
]
},
{
"sname":"Introduction to Communication Management",
"id":"5",
"vid":[
{
"url":"www.abcd.co"
},
{
"url":"www.xyz.com"
}
]
},
{
"sname":"Final Communication Management",
"id":"6",
"vid":[
{
"url":"www.abcd.co"
},
{
"url":"www.xyz.com"
}
]
},
{
"sname":"Modules",
"id":"7",
"vid":[
{
"url":"www.abcd.co"
},
{
"url":"www.xyz.com"
}
]
}
]
},
{
"name":"Cost Management",
"sub1":[
{
"sname":"Introduction to Cost Management",
"id":"8",
"vid":[
{
"url":"www.abcd.co"
},
{
"url":"www.xyz.com"
}
]
},
{
"sname":"Cost Management Introduction",
"id":"9",
"vid":[
{
"url":"www.abcd.co"
},
{
"url":"www.xyz.com"
}
]
}
]
}
]
<div ng-repeat="item_nm in test">{{item_nm.name}}
<div ng-repeat="sub1_dt in item_nm.sub1">
{{sub1_dt.sname}}
<div ng-repeat="vid in sub1_dt.vid">
{{vid.url}}
</div>
</div>
</div>
This will display name and corresponding sname and its url.

Resources