Grep JSON数组列表中的特定对象值

时间:2014-12-30 00:38:27

标签: python arrays json parsing

我有以下JSON响应数组列表。从中需要为特定服务角色提取jvirtualMachineId键的值。我如何获取一个元素的数组值。例如,我如何获取包含服务角色中的分析DB的jvirtualMachineId的元素值。感谢你的帮助..

[
    {
        "dataCenterId": 1,
        "customerInstallationId": 27355,
        "addressBlockId": 535,
        "cloudHostId": 31614,
        "hostname": "-94-t-jd-am",
        "ipAddresses": [
            {
                "machineAddressId": 65097,
                "addressRangeType": "NORMAL",
                "address": "103.191.23.20"
            },
            {
                "machineAddressId": 65098,
                "addressRangeType": "NFS",
                "address": "103.191.87.20"
            }
        ],
        "machineState": "RUNNING",
        "machineType": "THUNDER",
        "standbyVirtualMachineId": -1,
        "serviceRoles": [
            "ANALYTICS_DB",
            "SBS_DB"
        ],
        "hazduPen": false,
        "jHost": {
            "cloudHostId": 31614,
            "hostName": "d.dy.com",
            "ipAddress": "103.191.23.20",
            "shortHostname": "12-25-94-ad"
        },
        "jvirtualMachineId": 32551,
        "shortHostname": "-25-94-jd-ad"
    },
    {
        "dataCenterId": 1,
        "customerInstallationId": 27355,
        "addressBlockId": 535,
        "cloudHostId": 31613,
        "hostname": "ea01fdgdf.com",
        "ipAddresses": [
            {
                "machineAddressId": 65095,
                "addressRangeType": "NORMAL",
                "address": "103.191.23.19"
            },
            {
                "machineAddressId": 65096,
                "addressRangeType": "NFS",
                "address": "103.191.87.19"
            }
        ],
        "machineState": "RUNNING",
        "machineType": "THUNDER",
        "standbyVirtualMachineId": -1,
        "serviceRoles": [
            "EAE_SERVICE"
        ],
        "hazduPen": false,
        "jHost": {
            "cloudHostId": 31613,
            "hostName": ".dynjcdfgintom",
            "ipAddress": "103.191.23.19",
            "shortHostname": "-25-94-ser"
        },
        "jvirtualMachineId": 32550,
        "shortHostname": "esrsf-ea01"
    },
    {
        "dataCenterId": 1,
        "customerInstallationId": 27355,
        "addressBlockId": 535,
        "cloudHostId": 31612,
        "hostname": "25-94-t-csom",
        "ipAddresses": [
            {
                "machineAddressId": 65093,
                "addressRangeType": "NORMAL",
                "address": "103.191.23.18"
            },
            {
                "machineAddressId": 65094,
                "addressRangeType": "NFS",
                "address": "103.191.87.18"
            }
        ],
        "machineState": "RUNNING",
        "machineType": "THUNDER",
        "standbyVirtualMachineId": -1,
        "serviceRoles": [
            "CACHE"
        ],
        "hazduPen": false,
        "jiveHost": {
            "cloudHostId": 31612,
            "hostName": "sdfsdf4-t-cs01ted.com",
            "ipAddress": "103.191.23.18",
            "shortHostname": "sdfsdf25-94-t-cs01"
        },
        "jvirtualMachineId": 32549,
        "shortHostname": "12-25-94-t-cs01"
    }
]

3 个答案:

答案 0 :(得分:0)

您还没有告诉我们您需要使用哪种编程语言,所以我认为任何语言都可以。

通常,如果有任何工具可以做,我尝试用bash解决简单问题。幸运的是,有一个工具使你指定的任务变得简单:jq!以下是您需要做的事情:

cat data.json | jq '.[].serviceRoles="ANALYTICS_DB"|.[].jvirtualMachineId'

你提供的JSON实际上并不是很好的形式,所以我试着稍微阅读一下这些行:你期望一个对象数组。我可能猜错了,在这种情况下你可能需要稍微使用jq访问器。

答案 1 :(得分:0)

PHP类似于:

$element = what element you want to retrieve;

//fetching the file:

$jsonArray = json_decode(file_get_contents("content/coordinaten.json"));

echo 'value: '.$jsonArray[$element];

or in your specific question:

echo 'value: '.$jsonArray["jvirtualMachineId"];

答案 2 :(得分:0)

中使用

perl -MJSON -0ne '
    my $DS = decode_json $_;
    foreach my $h (@{ $DS }) {
        print $h->{jvirtualMachineId}
            if grep { /ANALYTICS_DB/ } @{ $h->{serviceRoles} };
    }
' file.json

输出:

32551