json-query 遍历数组

huangapple go评论69阅读模式
英文:

json-query to iterate through an array

问题

I want to iterate through the 'resources' array in the 'data' object and get the below result:

[
"ami-094axxxx",
"ami-0443c"
]

英文:

I have the below JavaScript file. I want to iterate through the "data" object and print the "ami" values. However, the query is not working. 'result.value' is null. I am not sure what I have done wrong.

What I have so far:

var jsonQuery = require('json-query');

var data = {
    "version": 3,
    "terraform_version": "0.12.31",
    "serial": 1,    
    "modules": [
        {
            "path": [
                "root"
            ],
            "outputs": {
                "aws_instance_tfer--i-0022xxxxxxxxeaf_Oracle-Dev_id": {
                    "sensitive": false,
                    "type": "string",
                    "value": "i-0022xxx"
                },
                "aws_instance_tfer--i-0100xxx_SRE-AMPP02_id": {
                    "sensitive": false,
                    "type": "string",
                    "value": "i-01000xxx"
                },
                "aws_instance_tfer--i-0142545fe3066b603_test-windows-agent_id": {
                    "sensitive": false,
                    "type": "string",
                    "value": "i-01425xxxxx3"
                }
            },
            "resources": {
                "aws_instance.tfer--i-0022xxxxxaf_Oracle-Dev-CMS": {
                    "type": "aws_instance",
                    "depends_on": [],
                    "primary": {
                        "id": "i-00221xxxxx",
                        "attributes": {
                            "ami": "ami-094axxxx",
                            "arn": "arn:aws",
                            "associate_public_ip_address": "false",
                            "availability_zone": "us-east",
                            "vpc_security_group_ids.0": "sg-0284ccc"
                        },
                        "meta": {
                            "schema_version": 1
                        },
                        "tainted": false
                    },
                    "deposed": [],
                    "provider": "provider.aws"
                },
                "aws_instance.tfer--i-0100xxxx_SRE-AMVAPP02": {
                    "type": "aws_instance",
                    "depends_on": [],
                    "primary": {
                        "id": "i-010001xxxxx",
                        "attributes": {
                            "ami": "ami-0443c",
                            "arn": "arn:aws4",
                            "associate_public_ip_address": "false",
                            "availability_zone": "us-east"    
                        },
                        "meta": {
                            "schema_version": 1
                        },
                        "tainted": false
                    },
                    "deposed": [],
                    "provider": "provider.aws"
                }
            },
            "depends_on": []
        }
    ]
}


var result = jsonQuery('modules[0].resources[*type=aws_instance].primary.attributes.ami', {data: data});


console.log(result.value);

I want to iterate through the 'resources' array in the 'data' object and get the below result:

[
"ami-094axxxx",
"ami-0443c"
]

答案1

得分: 1

以下是您提供的JavaScript代码的翻译部分:

您可以使用纯JavaScript完成此操作

// 开始代码片段
// 语言:JavaScript
var data = {
    "version": 3,
    "terraform_version": "0.12.31",
    "serial": 1,
    "modules": [
        {
            "path": [
                "root"
            ],
            "outputs": {
                "aws_instance_tfer--i-0022xxxxxxxxeaf_Oracle-Dev_id": {
                    "sensitive": false,
                    "type": "string",
                    "value": "i-0022xxx"
                },
                "aws_instance_tfer--i-0100xxx_SRE-AMPP02_id": {
                    "sensitive": false,
                    "type": "string",
                    "value": "i-01000xxx"
                },
                "aws_instance_tfer--i-0142545fe3066b603_test-windows-agent_id": {
                    "sensitive": false,
                    "type": "string",
                    "value": "i-01425xxxxx3"
                }
            },
            "resources": {
                "aws_instance.tfer--i-0022xxxxxaf_Oracle-Dev-CMS": {
                    "type": "aws_instance",
                    "depends_on": [],
                    "primary": {
                        "id": "i-00221xxxxx",
                        "attributes": {
                            "ami": "ami-094axxxx",
                            "arn": "arn:aws",
                            "associate_public_ip_address": "false",
                            "availability_zone": "us-east",
                            "vpc_security_group_ids.0": "sg-0284ccc"
                        },
                        "meta": {
                            "schema_version": 1
                        },
                        "tainted": false
                    },
                    "deposed": [],
                    "provider": "provider.aws"
                },
                "aws_instance.tfer--i-0100xxxx_SRE-AMVAPP02": {
                    "type": "aws_instance",
                    "depends_on": [],
                    "primary": {
                        "id": "i-010001xxxxx",
                        "attributes": {
                            "ami": "ami-0443c",
                            "arn": "arn:aws4",
                            "associate_public_ip_address": "false",
                            "availability_zone": "us-east"
                        },
                        "meta": {
                            "schema_version": 1
                        },
                        "tainted": false
                    },
                    "deposed": [],
                    "provider": "provider.aws"
                }
            },
            "depends_on": []
        }
    ]
}

const res =
Object.values(data.modules[0].resources)
  .filter(e => e.type == "aws_instance")
  .map(e => e.primary.attributes.ami);

console.log(res);
// 结束代码片段

这是您提供的JavaScript代码的中文翻译部分。如果您需要进一步的帮助或有其他问题,请随时提出。

英文:

You can do it with plain JavaScript:

<!-- begin snippet:js console:true -->
<!-- language:lang-js -->
var data = {
"version": 3,
"terraform_version": "0.12.31",
"serial": 1,
"modules": [
{
"path": [
"root"
],
"outputs": {
"aws_instance_tfer--i-0022xxxxxxxxeaf_Oracle-Dev_id": {
"sensitive": false,
"type": "string",
"value": "i-0022xxx"
},
"aws_instance_tfer--i-0100xxx_SRE-AMPP02_id": {
"sensitive": false,
"type": "string",
"value": "i-01000xxx"
},
"aws_instance_tfer--i-0142545fe3066b603_test-windows-agent_id": {
"sensitive": false,
"type": "string",
"value": "i-01425xxxxx3"
}
},
"resources": {
"aws_instance.tfer--i-0022xxxxxaf_Oracle-Dev-CMS": {
"type": "aws_instance",
"depends_on": [],
"primary": {
"id": "i-00221xxxxx",
"attributes": {
"ami": "ami-094axxxx",
"arn": "arn:aws",
"associate_public_ip_address": "false",
"availability_zone": "us-east",
"vpc_security_group_ids.0": "sg-0284ccc"
},
"meta": {
"schema_version": 1
},
"tainted": false
},
"deposed": [],
"provider": "provider.aws"
},
"aws_instance.tfer--i-0100xxxx_SRE-AMVAPP02": {
"type": "aws_instance",
"depends_on": [],
"primary": {
"id": "i-010001xxxxx",
"attributes": {
"ami": "ami-0443c",
"arn": "arn:aws4",
"associate_public_ip_address": "false",
"availability_zone": "us-east"
},
"meta": {
"schema_version": 1
},
"tainted": false
},
"deposed": [],
"provider": "provider.aws"
}
},
"depends_on": []
}
]
}

const res=
Object.values(data.modules[0].resources)
.filter(e=>e.type=="aws_instance")
.map(e=>e.primary.attributes.ami);

console.log(res);
<!-- end snippet -->

The values of the .resources object need to be extracted with Object.values() in order to get an array over which we can then .filter() and .map() the appropriate properties.

huangapple
  • 本文由 发表于 2023年5月7日 23:57:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定