I am trying to make discoverig instances from AWS Auto Scaling Group.
I have json like this:
{
"AutoScalingGroups": [
{
"AutoScalingGroupName": "xxx",
"AutoScalingGroupARN": "arn:aws:autoscaling:eu-central-1:xxx",
"LaunchTemplate": {
"LaunchTemplateId": "lt-xxx",
"LaunchTemplateName": "xxx",
"Version": "$Latest"
},
"MinSize": 2,
"MaxSize": 10,
"DesiredCapacity": 2,
"DefaultCooldown": 300,
"AvailabilityZones": [
"eu-central-1a",
"eu-central-1c",
"eu-central-1b"
],
"LoadBalancerNames": [],
"TargetGroupARNs": [],
"HealthCheckType": "EC2",
"HealthCheckGracePeriod": 0,
"Instances": [
{
"InstanceId": "i-xxx111",
"InstanceType": "c5.2xlarge",
"AvailabilityZone": "eu-central-1b",
"LifecycleState": "InService",
"HealthStatus": "Healthy",
"LaunchTemplate": {
"LaunchTemplateId": "lt-xxx",
"LaunchTemplateName": "xxx",
"Version": "11"
},
"ProtectedFromScaleIn": false
},
{
"InstanceId": "i-xxx222",
"InstanceType": "c5.2xlarge",
"AvailabilityZone": "eu-central-1a",
"LifecycleState": "InService",
"HealthStatus": "Healthy",
"LaunchTemplate": {
"LaunchTemplateId": "lt-xxx",
"LaunchTemplateName": "xxx",
"Version": "11"
},
"ProtectedFromScaleIn": false
}
]
}
]
}
And I want to create discovered items with name of instance id and values - healthstatus.
I created discovery rule with master item as previous json. And i got instance ids with item prototype that have next preprocessing JSONPath - $.AutoScalingGroups.[*].Instances.[*].InstanceId
This item has value next format ["i-xxx111","i-xxx222"], but i dont understand how to create next item prototype with names as each instance-id from this list.
So, can someone help me?
I am using zabbix version 6.2.4
You need to turn that object in the Zabbix LLD format. Using JSONpath won't help us in this case. You can use a JavaScript preprocessing in the Zabbix Item.
res = []
obj = JSON.parse(value)
for (asg in obj.AutoScalingGroups) {
for (i in obj.AutoScalingGroups[asg].Instances) {
res.push({"id": obj.AutoScalingGroups[asg].Instances[i].InstanceId})
}
}
return JSON.stringify(res)
result: [{"id":"i-xxx111"},{"id":"i-xxx222"}]
Don't forget to turn $.id in {#ID} in Zabbix LLD macro.
(Would be easier if we could use for (.. of ..) in ducktape...)
Here's a fiddle: https://jsfiddle.net/egch0vap/1/
See: https://www.zabbix.com/documentation/current/en/manual/config/items/preprocessing/javascript
Related
I am new to JSON Path and I am trying to get 'id' corresponding to name='Candy' using JsonPath in below JSON payload.
{
"responsePayload": [
{
"content": {
"id": "101",
"name": "Candy"
},
"links": [
{
"rel": "self",
"link": "api/v1/sweets/101",
"id": "101"
}
]
},
{
"content": {
"id": "102",
"name": "Chocolate"
},
"links": [
{
"rel": "self",
"link": "api/v1/sweets/102",
"id": "102"
}
]
}
]
}
For this I tried Jsonpath $.responsePayload[0].content[?(#.name=='Candy')].id but that is not working. I am using https://jsonpath.com/ for testing this JsonPath. Please guide me how I can achieve required result.
You're really close. You need more of the path inside the expression.
$.responsePayload[?(#.content.name=='Candy')].content.id
The [0] you have in your response isolates the first element only, but it sounds like you want to iterate over the whole array in responsePayload. To do that, the filter expression selector [?(...)] must act on that array.
Next, the # represents the current item, and you can build a full path off of it. You can see how that works above.
Finally, the filter expression selector returns the full item when the expression returns true. So you then need to navigate into .content.id again to get the value you're after.
So I have a JSON file I got from Postman which is returning as an empty object. This is how I'm reading it.
import regscooter from './json_files/reginald_griffin_scooter.json'
const scoot = regscooter;
const CustomerPage = () => {...}
reginald_griffin_scooter.json
{
"success": true,
"result": {
"id": "hhhhhhhhhh",
"model": "V1 Scooter",
"name": "hhhhhhhhhh",
"status": "active",
"availabilityStatus": "not-available",
"availabilityTrackingOn": true,
"serial": "hhhhhhhhhhhh",
"createdByUser": "hhhhhhhhK",
"createdByUsername": "hhhhhhhh",
"subAssets": [
"F0lOjWBAnG"
],
"parts": [
"hhhhhhhh"
],
"assignedCustomers": [
"hhhhhhhhh"
],
"createdAt": "2019-12-03T21:47:26.218Z",
"updatedAt": "2020-06-26T22:05:54.526Z",
"customFieldsAsset": [
{
"id": "hhhhhhh",
"name": "MAC",
"value": "hhhhhhhh",
"asset": "hhhhhhhhhh",
"user": "hhhhhhhhh",
"createdAt": "2019-12-03T21:47:26.342Z",
"updatedAt": "2019-12-11T16:29:24.732Z"
},
{
"id": "hhhhhhhh",
"name": "IMEI",
"value": "hhhhhhh",
"asset": "hhhhhhh",
"user": "hhhhhhhhhh",
"createdAt": "2019-12-03T21:47:26.342Z",
"updatedAt": "2019-12-11T16:29:24.834Z"
},
{
"id": "hhhhhhhhh",
"name": "Key Number",
"value": "NA",
"asset": "hhhhhhhhh",
"user": "hhhhhhhhhhh",
"createdAt": "2019-12-03T21:47:26.342Z",
"updatedAt": "2019-12-11T16:29:24.911Z"
}
]
}
}
The error is that "const scoot" is being shown as an empty object {}. I made sure to save a ton of times everywhere. I am able to read through the imported JSON file in other variables in similar ways, so I don't know why I can't parse this one. I just want to access the JSON object inside this. Also I omitted some information with hhhhh because of confidentiality.
EDIT: The code works, but it still has a red line beneath result when I do:
const scoot = regscooter.result.id;
It would be much more effective if you will provide an example in codesandbox or so.
However at first look it might be a parser issue ( maybe you are using Webpack with missing configuration for parsing files with json extension ), meaning we need more information to provide you with a full answer ( maybe solution ? ).
Have you tried to do the next:
const scoot = require('./json_files/reginald_griffin_scooter.json');
I have the following file "Pokemon.json", it's a stripped down list of Pokémon, listing their Pokédex ID, name and an array of Object Types.
[{
"name": "onix",
"id": 95,
"types": [{
"slot": 2,
"type": {
"name": "ground"
}
},
{
"slot": 1,
"type": {
"name": "rock"
}
}
]
}, {
"name": "drowzee",
"id": 96,
"types": [{
"slot": 1,
"type": {
"name": "psychic"
}
}]
}]
The output I'm trying to achieve is, extracting the name value of the type object and inserting it into an array.
I can easily get an array of all the types with
jq -r '.pokemon[].types[].type.name' pokemon.json
But I'm missing the key part to transform the name field into it's own array
[ {
"name": "onix",
"id": 95,
"types": [ "rock", "ground" ]
}, {
"name": "drowzee",
"id": 96,
"types": [ "psychic" ]
} ]
Any help appreciated, thank you!
In the man it states you have an option to use map - which essentially means walking over each result and returning something (in our case, same data, constructed differently.)
This means that for each row you are creating new object, and put some values inside
Pay attention, you do need another iterator within, since we want one object per row.
(we simply need to map the values in different way it is constructed right now.)
So the solution might look like so:
jq -r '.pokemon[]|{name:.name, id:.id, types:.types|map(.type.name)}' pokemon.json
I am trying to create an ARM template that will allow my to deploy VMs that may or may not need additional data disks. However when I test my template, I get the error below.
What is weird is that if the vmDataDisk parameter has a value of 0 or 1, all works perfectly. If that parameter has anything greater than 1, I get the error below.
For example the following works great: vmDataDisk = 1 and vmDataDiskSizesInGb = 30
However, the following values throw error below: vmDataDisk = 3, vmDataDiskSizesInGb = 10,20,30
Parameters Element - ARM json
"parameters": {
...
"vmDataDisks": {
"type": "int",
"defaultValue": 0,
"allowedValues": [
0,
1,
2,
3,
4,
5
],
"metadata": {
"description": "Select the number of data disks (in addition to OS disk) needed for this VM."
}
},
"vmDataDiskSizesInGb": {
"type": "string",
"defaultValue": 0,
"metadata": {
"description": "Enter string of comma separated values for the size of each data disk. For example, if the VmDataDisks parameter is set to '3', the VmDataDiskSizesInGb parameter might have a value of '10,25,50' and the template will deploy 3 data disks that are 10, 25 and 50 GB in size respectively."
}
}
}
Variables Element - ARM json
"variables": {
...
"diskSizes": "[split(parameters('vmDataDiskSizesInGb'), ',')]",
"copy": [
{
"name": "dataDisks",
"count": "[if(equals(parameters('vmDataDisks'),0), 1, parameters('vmDataDisks'))]",
"input": {
"name": "[concat(parameters('vmName'), '_DataDisk_', copyIndex('dataDisks'))]",
"lun": "[copyIndex('dataDisks')]",
"createOption": "Empty",
"diskSizeGB": "[if(equals(parameters('vmDataDisks'),0), 1, int(variables('diskSizes')[copyIndex()]))]",
"caching": "[if(startsWith(parameters('vmType'), 'SQL'), 'ReadOnly', 'None')]",
"managedDisk": {
"storageAccountType": "Premium_LRS"
}
}
}
]
}
Resources Element - ARM json
"resources": [
{
"type": "Microsoft.Compute/virtualMachines",
"name": "[parameters('vmName')]",
"apiVersion": "2017-03-30",
"location": "[parameters('location')]",
"dependsOn": [],
"properties": {
"hardwareProfile": {},
"osProfile": {},
"storageProfile": {
"imageReference": {},
"osDisk": {},
"dataDisks": "[if(equals(parameters('vmDataDisks'),0), json('null'), variables('dataDisks'))]"
},
"networkProfile": {},
"diagnosticsProfile": {}
}
}
]
Test-AzureRmResourceGroupDeployment Error
Code : InvalidTemplateMessage : Deployment template language
expression evaluation failed: 'The language expression property '0'
can't be evaluated.'. Please see
https://aka.ms/arm-template-expressions for usage details.Details :
The error says that the language expression you used in your template cannot be evaluated. For other words, the language expression you used does not meet the rules of the template. And the wrong expression property is ‘0’.
So you should check all your expression with property ‘0’ compared with the document that the error post: https://aka.ms/arm-template-expressions.
update
In the template code you post, the parameter "vmDataDiskSizesInGb" type is string and your "defaultValue" is 0, it's not correct, you should give "0" to it.
But I'm not sure if you have any other error,so I suggest you check all your expression with property ‘0’ compared with the document that the error post: https://aka.ms/arm-template-expressions.
Dears,
Can someone help me out reading the content of JSON file in LINUX machine without using JQ, Python & Ruby. Looking for purely SHELL scripting. We need to iterate the values if multiple records found. In the below case we have 2 set of records, which needs to iterated.
{
"version": [
"sessionrestore",1
],
"windows": [
{
"tabs": [
{
"entries": [
{
"url": "http://orf.at/#/stories/2.../" (http://orf.at/#/stories/2.../%27) ,
"title": "news.ORF.at",
"charset": "UTF-8",
"ID": 9588,
"docshellID": 298,
"docIdentifier": 10062,
"persist": true
},
{
"url": "http://oracle.at/#/stories/2.../" (http://oracle.at/#/stories/2.../%27) ,
"title": "news.at",
"charset": "UTF-8",
"ID": 9589,
"docshellID": 288,
"docIdentifier": 00062,
"persist": false
}
]
}
}
}