I have a json input which has few arrays inside an array . How do I loop inside the subarrays and get the key and values of all the data in subarrays in output?
this is the JSON input :
{
"Groups": [],
"Instances": [
{
"AmiLaunchIndex": 0,
"Architecture": "x86_64",
"BlockDeviceMappings": [
{
"DeviceName": "/dev/sda1",
"Ebs": {
"AttachTime": "2020-08-14 05:59:12+00:00",
"DeleteOnTermination": true,
"Status": "attached",
"VolumeId": "vol-032e47441"
}
},
{
"DeviceName": "xvdf",
"Ebs": {
"AttachTime": "2020-08-28 11:33:",
"DeleteOnTermination": false,
"Status": "attached",
"VolumeId": "vol-0f7b6c9"
}
}
],
"CapacityReservationSpecification": {
"CapacityReservationPreference": "open"
},
"ClientToken": "",
"CpuOptions": {
"CoreCount": 1,
"ThreadsPerCore": 1
},
"EbsOptimized": false,
"EnaSupport": true,
"EnclaveOptions": {
"Enabled": false
},
"HibernationOptions": {
"Configured": false
},
"Hypervisor": "abc",
"ImageId": "ami-040be",
"InstanceId": "i-0a19747c",
"InstanceType": "t2.micro",
"KeyName": "MyClan",
"LaunchTime": "2023-03-13 ",
"MaintenanceOptions": {
"AutoRecovery": "default"
},
"MetadataOptions": {
"HttpEndpoint": "enabled",
"HttpProtocolIpv6": "disabled",
"HttpPutResponseHopLimit": 1,
"HttpTokens": "optional",
"InstanceMetadataTags": "disabled",
"State": "applied"
},
"Monitoring": {
"State": "enabled"
},
"NetworkInterfaces": [
{
"Attachment": {
"AttachTime": "2020-08-14 05:59:",
"AttachmentId": "eni-attach-ecfb56",
"DeleteOnTermination": true,
"DeviceIndex": 0,
"NetworkCardIndex": 0,
"Status": "attached"
},
"Description": "",
"Groups": [
{
"GroupId": "sg-0d947",
"GroupName": "bc"
}
],
"InterfaceType": "interface",
"Ipv6Addresses": [],
"MacAddress": "12:36:47:bf:7a:0b",
"NetworkInterfaceId": "eni-0893e4f6",
"OwnerId": "01611",
"PrivateDnsName": "ip-10-87.ec2.internal",
"PrivateIpAddress": "10.87",
"PrivateIpAddresses": [
{
"Primary": true,
"PrivateDnsName": "ip1-87.ec2.internal",
"PrivateIpAddress": "10.87"
},
{
"Primary": false,
"PrivateDnsName": "ip-48.ec2.internal",
"PrivateIpAddress": "10.48"
}
],
"SourceDestCheck": true,
"Status": "in-use",
"SubnetId": "subnet-0b457b34d",
"VpcId": "vpc-05cc8"
}
],
"Placement": {
"AvailabilityZone": "us-south-3b",
"GroupName": "",
"Tenancy": "default"
},
"PlatformDetails": "abc",
"PrivateDnsName": "ip87.ec2.internal",
"PrivateDnsNameOptions": {},
"PrivateIpAddress": "10.7.9.87",
"ProductCodes": [],
"PublicDnsName": "",
"RootDeviceName": "/dev/sda1",
"RootDeviceType": "ebs",
"SecurityGroups": [
{
"GroupId": "sg-0d975c747",
"GroupName": "Bnm"
}
],
"SourceDestCheck": true,
"State": {
"Code": 16,
"Name": "running"
},
"StateTransitionReason": "",
"SubnetId": "subnet-0b234d",
"Tags": [
{
"Key": "MakeSnapshot",
"Value": "true"
},
{
"Key": "Owner",
"Value": "Amitdw"
},
{
"Key": "Expiry Date",
"Value": "12-Jun-2024"
},
{
"Key": "vmname",
"Value": "i-0a7b19747c"
},
{
"Key": "Name",
"Value": "fcva"
},
{
"Key": "Purpose",
"Value": "Performance Data"
}
],
"UsageOperation": "RunInstances:00",
"UsageOperationUpdateTime": "2020-08-14 05:59:11+00:00",
"VirtualizationType": "hvm",
"VpcId": "vpc-058dcc8"
}
],
"OwnerId": "0161",
"ReservationId": "r-02469ebf"
}
My spec :
I have tried to loop in the first array and got the 2 keys and values in the output , I am unable to enter the subarray even after mentioning the array name and * operation
[
{
"operation": "shift",
"spec": {
"Instances": {
"*": {
"AmiLaunchIndex": "[&1].AmiLaunchIndex",
"Architecture": "[&1].Architecture"
}
},
"BlockDeviceMappings": {
"*": {
"DeviceName": "[&1].DeviceName"
}
},
"Ebs": {
"*": {
"AttachTime": "[&1].AttachTime",
"DeleteOnTermination": "[&1].DeleteOnTermination",
"Status": "[&1].Status",
"VolumeId": "[&1].VolumeId"
}
}
}
}
]
My output :
( The output is limited only to the first array , it is not iterating the subarrays which were looped in the code )
[
{
"AmiLaunchIndex" : 0,
"Architecture" : "x86_64"
}
]
It is not looping past the first array.There are multiple arrays and subarrays in the json input. How do I iterate the arrays in the shift operation and get the correct key and values in the required output
required output :
[
{
"ReservationId":"r-00347618e087e",
"OwnerId":"016702611",
"GroupSet:"",
"InstancesSet_item_instanceId":"i-0560ce5b0",
"InstancesSet_item_ImageId":"ami-0ea6a9ded".
"InstancesSet_Item_InstanceState_code":"80",
"InstancesSet_Item_InstanceState_name":"stopped",
"InstancesSet_Item_PrivateDnsName":["ip-10-7-20.internal","ip-10-7-20.internal".....],
"InstancesSet_Item_DnsName":["ip-10-7-20.internal","ip-10-7-20.internal"......],
"InstancesSet_Item_Reason":"",
"InstancesSet_Item_KeyName":"ashok",
"InstancesSet_Item_AmiLaunchIndex":"0",
"InstancesSet_Item_ProductCodes":[],
"InstancesSet_Item_InstanceType":"t2.medium",
"InstancesSet_Item_LaunchTime":"2023-07-17 06:31:04+00:00",
"InstancesSet_Item_Placement_AvailabilityZone":"us-east-1c",
"InstancesSet_Item_Placement_GroupName":"default",
"InstancesSet_Item_Placement_Tenancy":"default",
"InstancesSet_Item_Monitoring_State:"disabled",
"InstancesSet_Item_SubnetId": "subnet-0d19137b8e40",
"InstancesSet_Item_VpcId":"vpc-054781249cc8",
"InstancesSet_Item_PrivateIpAddress":"10.7.20.257",
"InstancesSet_Item_SourceDestCheck": "true",
"InstancesSet_Item_GroupSet_Item_GroupId":"sg-015e76c6239b"
"InstancesSet_Item_GroupSet_Item_GroupName":"default",
"InstancesSet_Item_Architecture:"x86_64",
"InstancesSet_Item_RootDeviceType":"eb",
"InstancesSet_Item_RootDeviceName": "/dev/sda1",
"InstancesSet_Item_VirtualizationType":"hvm",
"InstancesSet_Item_ClientToken":"7d9e6e89-89ba-4b9865b0f8796",
"InstancesSet_Item_Hypervisor":"xen",
"InstancesSet_Item_NetworkInterfaceSet_Item_NetworkInterfaceId":"eni-0481a1b15f",
"InstancesSet_Item_NetworkInterfaceSet_Item_SubnetId":"subnet-0d19137af7,
"InstancesSet_Item_NetworkInterfaceSet_Item_VpcId":"vpc-054781249208",
"InstancesSet_Item_NetworkInterfaceSet_Item_Description":"",
"InstancesSet_Item_NetworkInterfaceSet_Item_OwnerId":"01670611",
"InstancesSet_Item_NetworkInterfaceSet_Item_Status":"in-use",
"InstancesSet_Item_NetworkInterfaceSet_Item_MacAddress":"0e:a5:d9:b1:5f",
"InstancesSet_Item_NetworkInterfaceSet_Item_PrivateIpAddress":"10.7.247",
"InstancesSet_Item_NetworkInterfaceSet_Item_PrivateDnsName":"ip-10-7-247.ec2.internal",
"InstancesSet_Item_NetworkInterfaceSet_Item_SourceDestCheck":"true",
"InstancesSet_Item_NetworkInterfaceSet_Item_GroupSet_Item_GroupId":"sg-015eb6239b",
"InstancesSet_Item_NetworkInterfaceSet_Item_GroupSet_Item_GroupName":"default",
"InstancesSet_Item_NetworkInterfaceSet_Item_Attachment_AttachmentId":"eni-attach-085ab400ca0a"
"InstancesSet_Item_NetworkInterfaceSet_Item_Attachment_DeviceIndex":"0",
"InstancesSet_Item_NetworkInterfaceSet_Item_Attachment_Status":"attached",
"InstancesSet_Item_NetworkInterfaceSet_Item_Attachment_AttachTime":"2023-07-13 06:51:15+00:00",
"InstancesSet_Item_NetworkInterfaceSet_Item_Attachment_DeleteOnTermination":"true",
"InstancesSet_Item_NetworkInterfaceSet_Item_Attachment_NetworkCardIndex":"0",
"InstancesSet_Item_NetworkInterfaceSet_Item_Ipv6AddressesSet":"[ ]",
"InstancesSet_Item_NetworkInterfaceSet_Item_InterfaceType":"interface",
"InstancesSet_Item_EbsOptimized:"false",
"InstancesSet_Item_EnaSupport":"true",
"InstancesSet_Item_CpuOptions_CoreCount:"2",
"InstancesSet_Item_CpuOptions_ThreadsPerCore:"1",
"InstancesSet_Item_CapacityReservationSpecification_CapacityReservationPreference:"open",
"InstancesSet_Item_HibernationOptions_Configured:"false",
"InstancesSet_Item_EnclaveOptions_Enabled":"false",
"InstancesSet_Item_metadataOptions_State":"applied",
"InstancesSet_Item_metadataOptions_HttpTokens":"optional",
"InstancesSet_Item_metadataOptions_HttpPutResponseHopLimit":"1",
"InstancesSet_Item_metadataOptions_HttpEndpoint":"enabled",
"InstancesSet_TagSet_Key":["name","purpose"],
"InstancesSet_TagSet_value":["test1","Disk"],
"InstancesSet_Item_Platform":"windows",
"InstancesSet_Item_StateReason_Code": "Client.UserInitiatedShutdown",
"InstancesSet_Item_StateReason_Message":"Client.UserInitiatedShutdown: User initiated shutdown",
"InstancesSet_Item_BootMode":"legacy-bios",
"InstancesSet_Item_BlockDeviceMapping_Item_DeviceName":["/dev/sda1", "xvdf","xvdg","xvdh"],
"InstancesSet_Item_BlockDeviceMapping_Item_Ebs_VolumeId":["vol-0df9d3b259d7c3","vol-0b037b1913568c","vol-09a1c38ef279e","vol-0786e6c0889ebb"],
"InstancesSet_Item_BlockDeviceMapping_Item_Ebs_Status":["attached","attached","attached"],
"InstancesSet_Item_BlockDeviceMapping_Item_Ebs_AttachTime":["2023-07-14 12:10:22+00:00","2023-07-14 12:10:40+00:00","2023-07-14 12:10:51+00:00"],
"InstancesSet_Item_BlockDeviceMapping_Item_ebs_deleteOnTermination":["true","false","false","false"],
"InstancesSet_Item_NetworkInterfaceSet_Item_PrivateIpAddressesSet_Item_PrivateIpAddress:"10.7.247",
"InstancesSet_Item_NetworkInterfaceSet_Item_PrivateIpAddressesSet_Item_PrivateDnsName":"ip-10-7-247.ec2.internal",
"InstancesSet_Item_NetworkInterfaceSet_Item_PrivateIpAddressesSet_Item_Primary":"true",
"InstancesSet_Item_IamInstanceProfile_Arn":""arn:aws:iam::01672611:instance-profile/SSMaccess",
"InstancesSet_Item_IamInstanceProfile_Id":"AIPAQHYOHUOY",
"CustomerName":"",
"ServiceName":"",
"instancesSet_item_platformDetails":"Windows",
"instancesSet_item_usageOperation":"RunInstances:0002",
"InstancesSet_Item_TagSet_Item":"",
"CustomerDisplayName":"",
"instancesSet_item_instanceState":"",
"instancesSet_item_placement":"",
"instancesSet_item_monitoring":"",
"instancesSet_item_ipAddress":"",
"instancesSet_item_groupSet":"",
"instancesSet_item_blockDeviceMapping":"",
"instancesSet_item_tagSet":"",
"instancesSet_item_networkInterfaceSet":"",
"instancesSet_item_iamInstanceProfile":"",
"instancesSet_item_cpuOptions":"",
"instancesSet_item_capacityReservationSpecification":"",
"instancesSet_item_hibernationOptions":"",
"instancesSet_item_enclaveOptions":"",
"instancesSet_item_metadataOptions":"",
"instancesSet_item_maintenanceOptions":"",
"instancesSet_item_usageOperationUpdateTime":"",
"instancesSet_item_privateDnsNameOptions":"",
"requesterId":"",
"instancesSet_item_stateReason":"",
"instancesSet_item_currentInstanceBootMode":"",
"EngagementId":"",
"Sign":""
"Version":"1"
}
]
Is this what you are looking for?
[
{
"operation": "shift",
"spec": {
"Instances": {
"*": {
"AmiLaunchIndex": "[&1].AmiLaunchIndex",
"Architecture": "[&1].Architecture",
"BlockDeviceMappings": {
"*": {
"DeviceName": "[&3].DeviceName",
"Ebs": {
"AttachTime": "[&4].AttachTime",
"DeleteOnTermination": "[&4].DeleteOnTermination",
"Status": "[&4].Status",
"VolumeId": "[&4].VolumeId"
}
}
}
}
}
}
}
]
OUTPUT:
[ {
"AmiLaunchIndex" : 0,
"Architecture" : "x86_64",
"DeviceName" : [ "/dev/sda1", "xvdf" ],
"AttachTime" : [ "2020-08-14 05:59:12+00:00", "2020-08-28 11:33:" ],
"DeleteOnTermination" : [ true, false ],
"Status" : [ "attached", "attached" ],
"VolumeId" : [ "vol-032e47441", "vol-0f7b6c9" ]
} ]