How to create table separate objects out of json using JSONATA - json

I want to transform a given JSON structure into another json format using the JSONATA API.
Basically, I need to break the hierarchical structure into separate records.
Input JSON:
{
"2022-09-22": [
{
"name": "modules/dynatrace",
"count": 60
},
{
"name": "modules/dynatrace/monitors/http-monitors/basic",
"count": 4
},
{
"name": "modules/splunk/hec-token",
"count": 14
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 29
}
]
}
Output:
[
{
"date" : "2022-09-22",
"name": "modules/dynatrace",
"count": 60
},
{
"date" : "2022-09-22",
"name": "modules/dynatrace/monitors/http-monitors/basic",
"count": 4
},
{
"date" : "2022-09-22",
"name": "modules/splunk/hec-token",
"count": 14
},
{
"date" : "2022-09-22",
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 29
}
]

You can use the $each function to convert object into an array:
$each($$, function($entries, $date) {
$entries.($merge([{ "date": $date }, $]))
})
Interactive link: https://stedi.link/ZBoBY2F

Related

How to project column name and value from JSON in KQL?

I have the following 'SetOfSignals' in KQL (using mv-expand):
"SetOfSignals": {
"name": "CompanyName",
"signals": [
{
"name": "AmbientAirTemperature",
"unit": "C",
"dataType": "Float32",
"values": [
"11.5"
]
},
{
"name": "AverageEnergyConsumption",
"unit": "W",
"dataType": "Float32",
"values": [
"780.0"
]
}
}
and now I want to project the signal names with corresponding values.
I want it to look like this:
...
AmbientAirTemperature
AverageEnergyConsumption
...
11.5
780.0
but using something like | extend AmbientAirTemperature = signals.name doesn't works since there are multiple strings within "signals" with the name "name".
Thanks.
datatable(SetOfSignals:dynamic)
[
dynamic
(
{
"name": "CompanyName",
"signals": [
{
"name": "AmbientAirTemperature",
"unit": "C",
"dataType": "Float32",
"values": [
"11.5"
]
},
{
"name": "AverageEnergyConsumption",
"unit": "W",
"dataType": "Float32",
"values": [
"780.0"
]
}
]
}
)
]
| mv-apply signal = SetOfSignals.signals on
(
summarize make_bag(bag_pack(tostring(signal.name), signal.values[0]))
)
| project-away SetOfSignals
| evaluate bag_unpack(bag_)
AmbientAirTemperature
AverageEnergyConsumption
11.5
780.0
Fiddle

How to remove field in nested json array by using MySQL functions?

Lets say I have the following JSON structure in my DB:
{
"user": "test",
"cars": [
{
"name": "car1",
"drivers": [
{
"name": "name1",
"age": 18
},
{
"name": "name2",
"age": 21
}
]
},
{
"name": "car2",
"drivers": [
{
"name": "name3",
"age": 25
}
]
}
]
}
I want to remove all the age fields in the nested arrays?
How can I achieve this via MySQL JSON functions?

Select value from array json using jq

I have the following json file which this value
{
"data": [
{
"id": 60573936,
"timeCreated": 1615458564202,
"timeUpdated": 1615458564202,
"name": "name1",
"desiredStatus": "UNMANAGED",
"defaultStatus": "STARTED",
"targets": [
{
"id": 2520949,
"lastReportedStatus": "STOPPED"
}, {
"id": 702881,
"lastReportedStatus": "STARTED"
}
]
}, {
"id": 60574370,
"timeCreated": 1615458812565,
"timeUpdated": 1615458812565,
"name": "name2",
"desiredStatus": "UNMANAGED",
"defaultStatus": "STARTED",
"targets": [
{
"id": 2520949,
"lastReportedStatus": "STARTED"
}, {
"id": 702881,
"lastReportedStatus": "STOPPED"
}
]
}, {
"id": 60574329,
"timeCreated": 1615458775053,
"timeUpdated": 1615458775053,
"name": "name3",
"desiredStatus": "UNMANAGED",
"defaultStatus": "STARTED",
"targets": [
{
"id": 2520949,
"lastReportedStatus": "STOPPED"
}, {
"id": 702881,
"lastReportedStatus": "STARTED"
}
]
}
]
}
I would like to extract the "name" value only if "id"="2520949" and "lastReportedStatus"="STOPPED" using jq. In my example I would like to get "name1" and "name3". I tried using select feature but I'm not able to satisfy the "and" condition between "id" and "lastReportedStatus" key. What is the correct code with jq ?
jq '.data[]
| select(.targets[] | .lastReportedStatus=="STOPPED" and .id==2520949)
| .name' file.json

Expecting 'EOF', '}', ',', ']', got 'STRING'

I am trying to add some code but it turns out I cant add these two together, I am new to JSON and coding in general, anyhelp?
"playerStatProgression": [{
"name": "HalloweenKillerVialRepetitions"
}, {
"name": "HalloweenSurvivorVialRepetitions",
"value": 1
}, {
"name": "DBD_HalloweenSurvivorToxin"
}],
"specialEvent": [{
"eventId": "Halloween2018",
"seenCinematics": [0]
}],
"versionNumber": 7
}
"playerStatProgression": [{
"name": "DBD_GoldenCoin",
"value": 89
}, {
"name": "DBD_BurntCoin",
"value": 53
}, {
"name": "DBD_SummerKillerCoin",
"value": 71
}, {
"name": "DBD_SummerSurvivorCoin",
"value": 112
}], "versionNumber": 7
}
You're supposed to wrap your JSON object in curly braces ({}) because a JSON file can only contain one object. Also, it seems invalid. Put it through a validator to figure out the issues.
You can do this in this way:
[
{
"playerStatProgression": [
{
"name": "HalloweenKillerVialRepetitions"
},
{
"name": "HalloweenSurvivorVialRepetitions",
"value": 1
},
{
"name": "DBD_HalloweenSurvivorToxin"
}
],
"specialEvent": [
{
"eventId": "Halloween2018",
"seenCinematics": [
0
]
}
],
"versionNumber": 7
},
{
"playerStatProgression": [
{
"name": "DBD_GoldenCoin",
"value": 89
},
{
"name": "DBD_BurntCoin",
"value": 53
},
{
"name": "DBD_SummerKillerCoin",
"value": 71
},
{
"name": "DBD_SummerSurvivorCoin",
"value": 112
}
],
"versionNumber": 7
}
]

Fetch data based on key

{ "Labels": [ {"Test": 99.25341796875, "Name": "Skateboard" }, { "Test": 9.25341796875, "Name": "Sport" }, { "Test": 49.24723052978516, "Name": "People" }]}
I need to remove the Testtag on basis of below conditions
if Test value >50 then replace Test=Major
if Test value <50 then replace with Test=Minor
so here output requested is like below.
{ "Labels": [ {"High": 99.25341796875, "Name": "Skateboard" }, { "Low": 9.25341796875, "Name": "Sport" }, { "Low": 49.24723052978516, "Name": "People" }]}
jq solution:
jq '.Labels |= map(.[(if .Confidence > 50 then "High" else "Low" end)]= .Confidence | del(.Confidence))' yourfile.json
The output:
{
"Labels": [
{
"Name": "Skateboard",
"High": 99.25341796875
},
{
"Name": "Sport",
"Low": 9.25341796875
},
{
"Name": "People",
"Low": 49.24723052978516
}
]
}