Kusto query loop over json array - azure-data-explorer

I want to loop over a JSON array like this:
[
{
"id": 1,
"count" : 30
},
{
"id": 2,
"count" : 10
},
{
"id": 3,
"count" : 5
},
{
"id": 4,
"count" : 15
}
]
So I would like to have a query to project a TotalCount which would basically go over the json array and sum all the count values(30+10+5+15) and display as a new column

You can use mv-apply to do so.
For example:
datatable(d: dynamic) [
dynamic([
{
"id": 1,
"count": 30
},
{
"id": 2,
"count": 10
},
{
"id": 3,
"count": 5
},
{
"id": 4,
"count": 15
}
]),
dynamic([
{
"id": 1,
"count": 3
},
{
"id": 2,
"count": 1
},
{
"id": 3,
"count": 50
},
{
"id": 4,
"count": 1
}
]),
]
| mv-apply d on (
summarize result = sum(tolong(d['count']))
)
result
60
55

Related

Is there a way to transform these 2 arrays by using jq, into a set of objects, like in the example down below?

Example json data:
{
"data": [
{
"place": "FM346",
"id": [
"7_day_A",
"7_day_B",
"7_day_C",
"7_day_D"
],
"values": [
0,
30,
23,
43
]
},
{
"place": "LH210",
"id": [
"1_day_A",
"1_day_B",
"1_day_C",
"1_day_D"
],
"values": [
4,
45,
100,
9
]
}
]
}
what i need to transform it into:
{
"data": [
{
"place": "FM346",
"7_day_A": {
"value": 0
},
"7_day_B": {
"value": 30
},
"7_day_C": {
"value": 23
},
"7_day_D": {
"value": 43
}
},
{
"place": "LH210",
"1_day_A": {
"value": 4
},
"1_day_B": {
"value": 45
},
"1_day_C": {
"value": 100
},
"1_day_D": {
"value": 9
}
}
]
}
i have tried this:
{
data:[.data |.[]|
{
place: (.place),
(.id[]):
{
value: (.values[])
}
}]
}
(in jqplay: https://jqplay.org/s/f4BBtN9gwmp)
and this:
{
data:[.data |.[]|
{
place: (.place),
test:
[{
(.id[]):
{
value: (.values[])
}
}]
}]
}
(in jqplay: https://jqplay.org/s/pKIvQe1CzgX)
but they arent grouped in the way i wanted and it gives each value to each id, not the corresponding one.
I have been trying for some time now, but im new to jq and have no idea how to transform it this way, thanks in advance for any answers.
You can use transpose here, which can play a key role in converting the arrays to key/value pairs
.data[] |= {place} +
([ .id, .values ] | transpose | map({(.[0]): { value: .[1] } }) | add)
The solution works by converting the array-of-arrays [.id, .values] by transposing them, i.e. converting
[["7_day_A","7_day_B","7_day_C","7_day_D"],[0,30,23,43]]
[["1_day_A","1_day_B","1_day_C","1_day_D"],[4,45,100,9]]
to
[["7_day_A",0],["7_day_B",30],["7_day_C",23],["7_day_D",43]]
[["1_day_A",4],["1_day_B",45],["1_day_C",100],["1_day_D",9]]
With the transformation done, we construct an object with key as the zeroth index element and value as an object comprising of the value of first index element, and combine the results together with add
Demo - jqplay

Project certain column and carry on one entry in every row

I have a dataset which looks like the following
{
"metadata":"d_meta_v_1.5.9",
"data": {
"a": {
"T": [
1652167964645,
1652168781684,
1652168781720
],
"V": [
1,
2,
3
]
},
"b": {
"T": [
1652167961657,
1652168781720,
1652168781818
],
"V": [
1,
3,
4
]
},
"c": {
"T": [
1652167960194,
1652168787377
],
"V": [
1,
3
]
}
}
}
I want to select the certain column and carry on the metadata also at the end. a part of this question is working in my perviou question here
How can I get my desired output ?
Metadata, Time, a, b
d_meta_v_1.5.9, <Time>, <value of _a>, < value of b>
d_meta_v_1.5.9, <Time>, <value of _a>, < value of b>
d_meta_v_1.5.9, <Time>, <value of _a>, < value of b>
let requested_columns = dynamic(["a","b"]);
datatable(doc:dynamic)
[
dynamic
(
{
"metadata":"d_meta_v_1.5.9",
"data": {
"a": {
"T": [
1652167964645,
1652168781684,
1652168781720
],
"V": [
1,
2,
3
]
},
"b": {
"T": [
1652167961657,
1652168781720,
1652168781818
],
"V": [
1,
3,
4
]
},
"c": {
"T": [
1652167960194,
1652168787377
],
"V": [
1,
3
]
}
}
}
)
]
| project metadata = doc.metadata, data = doc.data
| mv-expand data = data
| extend key = tostring(bag_keys(data)[0])
| where key in (requested_columns)
| mv-expand T = data[key].T to typeof(long), V = data[key].V to typeof(long)
| evaluate pivot(key, take_any(V), metadata, T)
| order by T asc
metadata
T
a
b
d_meta_v_1.5.9
1652167961657
1
d_meta_v_1.5.9
1652167964645
1
d_meta_v_1.5.9
1652168781684
2
d_meta_v_1.5.9
1652168781720
3
3
d_meta_v_1.5.9
1652168781818
4
Fiddle

Projectig certain sub documents in Kusto

I have a timeseries data which looks like follows
"data": {
"a": {
"T": [
1652167964645,
1652168781684,
1652168781720,
1652169266156,
1652169267146,
1652169272796,
1652169299338
],
"V": [
1,
2,
3,
10,
6,
1252,
1555
]
},
"b": {
"T": [
1652167961657,
1652168781720,
1652168781818,
1652168787377,
1652168835734,
1652169266108,
1652169266125,
1652169272798,
1652169299328
],
"V": [
1,
3,
4,
6,
12,
15,
16,
17,
1
]
},
"c": {
"T": [
1652167960194,
1652168787377,
1652169266108,
1652169272798,
1652169299328
],
"V": [
1,
3,
17,
18,
1
]
}}
inside the sub documents there are time and values
I can process the data in total. but if I want tp process only two sub document how can i do that ?
I can project like following
| project data["a"],data["b"] but then I can not process the time. how can i accomplish it ?
Expected output:
One column with time, and other column ( i.e a, b ) for the values
Time , A , B
0:55, 1,2
let requested_columns = dynamic(["a","b"]);
datatable(data:dynamic)
[
dynamic
(
{
"a": {
"T": [
1652167964645,
1652168781684,
1652168781720,
1652169266156,
1652169267146,
1652169272796,
1652169299338
],
"V": [
1,
2,
3,
10,
6,
1252,
1555
]
},
"b": {
"T": [
1652167961657,
1652168781720,
1652168781818,
1652168787377,
1652168835734,
1652169266108,
1652169266125,
1652169272798,
1652169299328
],
"V": [
1,
3,
4,
6,
12,
15,
16,
17,
1
]
},
"c": {
"T": [
1652167960194,
1652168787377,
1652169266108,
1652169272798,
1652169299328
],
"V": [
1,
3,
17,
18,
1
]
}
}
)
]
| mv-expand data
| extend key = tostring(bag_keys(data)[0])
| where key in (requested_columns)
| mv-expand T = data[key].T to typeof(long), V = data[key].V to typeof(long)
| evaluate pivot(key, take_any(V), T)
| order by T asc
T
a
b
1652167961657
1
1652167964645
1
1652168781684
2
1652168781720
3
3
1652168781818
4
1652168787377
6
1652168835734
12
1652169266108
15
1652169266125
16
1652169266156
10
1652169267146
6
1652169272796
1252
1652169272798
17
1652169299328
1
1652169299338
1555
Fiddle

How to retrieve array element from a specific position in CosmosDB document?

Suppose I have a document with the following structure,
{
"VehicleDetailId": 1,
"VehicleDetail": [
{
"Id": 1,
"Make": "BMW"
},
{
"Id": 1,
"Model": "ABDS"
},
{
"Id": 1,
"Trim": "5.6L/ASMD"
},
{
"Id": 1,
"Year": 2008
}
]
}
Now I want to retrieve an array element located at a specific position from VehicleDetail array like I want to retrieve the second element, i.e.,
{
"Id": 1,
"Model": "ABDS"
}
or the third,
{
"Id": 1,
"Trim": "5.6L/ASMD"
}
How should I write the query to achieve this?
Use the built-in ARRAY_SLICE function. This allows you to select part of an array.
Pass the array, starting position, number of elements to select.
SELECT ARRAY_SLICE(c.VehicleDetail, 1, 1) As SecondElement
FROM c
Output:
{
"SecondElement": [
{
"Id": 1,
"Model": "ABDS"
}
]
}

How to get rid of the rows using sequelize

I am using sequelize.
const total = await models.parcel.findAndCountAll({ group: ['status'] });
And I got this response. But I want to only count not rows on response.
How can I make this? Thank you so much for reading my question.
{
"count": [
{
"status": null,
"count": 8
},
{
"status": "1",
"count": 5
},
{
"status": "2",
"count": 3
}
],
"rows": [
{
"id": 3,
"companyName": "hy",
"invoice": "904103",
"receiverName": "Rtrt",
},......
]
}
Just Change findAndCountAll to count:
await models.parcel.findAndCountAll({ group: ['status'] });
To :
await models.parcel.count({ group: ['status'] });

Resources