I have nested array of objects.
How can I filter by specific value in array? I want filter by reg = region1 and value greater than 2
{
"region": [
{
"reg": "region1",
"val": 2
},
{
"reg": "region2",
"val": 6
}
]
}
Related
I need to transform array to array with some extra logic:
Map field name in array if it exists in the mapping object, if not process it as it is in the source
Sum up values of objects with the same name
Remove objects with zero value
for example here is the source json:
{
"additive": [
{
"name": "field-1",
"volume": "10"
},
{
"name": "field-2",
"volume": "10"
},
{
"name": "field-3",
"volume": "0"
},
{
"name": "field-4",
"volume": "5"
}
]
}
object with mapping config(field-1 and field-2 is mapped to the same value):
{
"field-1": "field-1-mapped",
"field-2": "field-1-mapped",
"field-3": "field-3-mapped"
}
and this is the result that I need to have
{
"chemicals": [
{
"name": "field-1-mapped",
"value": 20
},
{
"name": "field-4",
"value": 5
}
]
}
as you can see field-1 and field-2 is mapped to field-1-mapped so the values are summed up, field-3 has 0 value so it is removed and field-4 is passed as it is because it's missing in the mapping.
so my question is: is it possible to make it with JSONata?
I have tried to make it work but I stuck with this lookup function that doesn't return default value when name is missing in mapping:
{
"chemicals": additive # $additive #$.{
"name": $res := $lookup({
"field-1": "field-1-mapped",
"field-2": "field-1-mapped",
"field-3": "field-3-mapped"
}, $additive.name)[ $res ? $res : $additive.name],
"amount": $number($additive.volume),
} [amount>0]
}
Probably easiest to break it down into steps as follows:
(
/* define lookup table */
$table := {
"field-1": "field-1-mapped",
"field-2": "field-1-mapped",
"field-3": "field-3-mapped"
};
/* substitute the name; if it's not in the table, just use the name */
$mapped := additive.{
"name": [$lookup($table, name), name][0],
"volume": $number(volume)
};
/* group by name, and aggregate the volumes */
$grouped := $mapped[volume > 0]{name: $sum(volume)};
/* convert back to array */
{
"chemicals": $each($grouped, function($v, $n) {{
"name": $n,
"volume": $v
}})
}
)
See https://try.jsonata.org/0BWeRcRoZ
I have a json data which is in the form of key and all values in a array but I need to transform it into a array of key value pairs, here is the data
Source data
"2022-08-30T06:58:56.573730Z": [
{ "tag": "AC 3 Phase/7957", "value": 161.37313113545272 },
{ "tag": "AC 3 Phase/7956", "value": 285.46869739695853 }
]
}
Transformation looking for
[
{ "tag": "AC 3 Phase/7957",
"ts": 2022-08-30T06:58:56.573730Z,
"value": 161.37313113545272
},
{ "tag": "AC 3 Phase/7956",
"ts": 2022-08-30T06:58:56.573730Z,
"value": 285.46869739695853
}
]
I would do it like this:
$each($$, function($entries, $ts) {
$entries.{
"tag": tag,
"ts": $ts,
"value": value
}
}) ~> $reduce($append, [])
Feel free to play with this example on the playground: https://stedi.link/g6qJGcP
I have the following input in Nifi Jolt Specification processor:
[
{
"values": [
{
"id": "paramA",
"value": 1
}
]
},
{
"values": [
{
"id": "paramB",
"value": 3
}
]
}
]
Expected output:
[
{
"id": "paramA",
"value": 1
},
{
"id": "paramB",
"value": 2
}
]
Can you explain how I have to do?
thanks in advance
You want to reach the objects of the values array which are nested within seperate object signs ({}). A "*" notation is needed in order to cross them over per each individual values array, and then use another "*" notation for indexes of those arrays while choosing "" as the counterpart values in order to grab nothing but the sub-objects such as
[
{
"operation": "shift",
"spec": {
"*": {
"values": {
"*": ""
}
}
}
}
]
I am working on Elasticsearch Aggregation and have a question regarding how to do pipeline sort of aggregation. I have three high-level fields in my ES document:
documentId, list1, list2
Example:
This is the couple of documents I have:
document 1:
{
"documentId":"1",
"list1":
[
{
"key": "key1",
"value": "value11"
}
],
"list2":
[
{
"key": "key2",
"value": "value21"
}
...
]
}
document 2:
{
"documentId":"2",
"list1":
[
{
"key": "key1",
"value": "value11"
}
],
"list2":
[
{
"key": "key2",
"value": "value21"
}
...
]
}
document 3:
{
"documentId":"3",
"list1":
[
{
"key": "key1",
"value": "value12"
}
],
"list2":
[
{
"key": "key2",
"value": "value21"
}
...
]
}
To summarize -
document1 and document2 has same set of values for key1 and key2 (Except id is different, so they are treated two separate documents).
document3 has same value for key2 as in document1 and document2. Value for key1 is different from document1 and document2.
I want to run terms aggregator on keys of list1 field which should go as input into terms aggregation done on list2.
So, for the above example, the overall output I want is -
value21: 2
(one count corresponding to value11 in key1 and second count corresponding to value12 in key1)
and NOT
value21: 3 (two counts corresponding to value11 in key1 and third count corresponding to value12 in key1).
Is there any simple way of doing this?
{
"id": 1,
"subdocuments": [
{
"id": "A",
"name": 1
},
{
"id": "B",
"name": 2
},
{
"id": "C",
"name": 3
}
]
}
How do update a subdocument "A"s "name" to a value of 2 in RethinkDB in either Javascript or Python?
If you can rely of the position of your "A " element you can update like this:
r.db("DB").table("TABLE").get(1)
.update({subdocuments:
r.row("subdocuments").changeAt(0, r.row("subdocuments").nth(0).merge({"name":2}))})
If you can not rely on the position, you have to find it yourself:
r.db("DB").table("TABLE").get(1).do(function(doc){
return doc("subdocuments").offsetsOf(function(sub){return sub("id").match("A")}).nth(0)
.do(function(index){
return r.db("DB").table("TABLE").update({"subdocuments":
doc("subdocuments").changeAt(index, doc("subdocuments").nth(index).merge({"name":2})) })})
})
As an alternative you can use the map function to iterate over the array elements and update the one that matches your condition
r.db("DB").table("TABLE").get(1)
.update({
subdocuments: r.row("subdocuments").map(function(sub){
return r.branch(sub("id").eq("A"), sub.merge({name: 2}), sub)
})
})