小编典典

使用Bash Shell提取JSON对象?

json

我有一个以下json文件,我正在寻找一种使用bash shell使用对象路径提取对象的方法。

例如,如果我说extract('production-ap/ap-northeast-1')的话,那将返回我"accessKey": "OO""accountID": "99"

我喜欢bash shell脚本,但了解有限,请帮忙!

谢谢

{
    "production-ap": {
        "ap-northeast-1": {
            "accessKey": "OO",
            "accountID": "99"
        },
        "ap-northeast-2": {
            "accessKey": "AB",
            "accountID": "12"
        }
    },
    "production-eu": {
        "eu-west-1": {
            "accessKey": "CD",
            "accountID": "34"
        },
        "us-east-1": {
            "accessKey": "CD",
            "accountID": "34"
        }
    },
    "production-us": {
        "us-east-1": {
            "accessKey": "EF",
            "accountID": "56"
        },
        "us-east-2": {
            "accessKey": "EF",
            "accountID": "56"
        }
    },
    "stage-ap": {
        "ap-northeast-1": {
            "accessKey": "AK",
            "accountID": "78"
        },
        "ap-northeast-2": {
            "accessKey": "AK",
            "accountID": "78"
        }
    },
    "stage-eu": {
        "eu-west-1": {
            "accessKey": "AK",
            "accountID": "55"
        },
        "eu-west-2": {
            "accessKey": "AK",
            "accountID": "55"
        }
    },
    "stage-us": {
        "us-east-1": {
            "accessKey": "AK",
            "accountID": "30"
        },
        "us-east-2": {
            "accessKey": "AK",
            "accountID": "30"
        }
    },
    "private": {
        "us-west-2": {
            "accessKey": "z2",
            "accountID": "52"
        },
        "us-west-1": {
            "accessKey": "z2",
            "accountID": "52"
        }
    }
}

阅读 761

收藏
2020-07-27

共1个答案

小编典典

使用bash这样做会很困难。尽管我不确定它们的可靠性,但似乎确实
JSON解析器编写为shell脚本。我建议像jq这样的东西可以作为单独的程序运行并在管道中使用。它是一个独立的可执行文件,用C语言编写。没有理由不能将它与程序一起提供。

2020-07-27