【问题标题】:Extract JSON object using Bash shell? [duplicate]使用 Bash shell 提取 JSON 对象? [复制]
【发布时间】:2014-04-14 12:47:29
【问题描述】:

我有一个以下 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"
        }
    }
}

【问题讨论】:

标签: json bash shell sed awk


【解决方案1】:

使用 bash 执行此操作会很不稳定。 do seem to be json 解析器编写为 shell 脚本,尽管我不确定它们有多可靠。我推荐像jq 这样的东西,它可以作为一个单独的程序运行并在管道中使用。它是一个独立的可执行文件,用 C 语言编写。它没有理由不能与您的程序一起发布。

【讨论】:

  • 看起来像您建议的 JSON.sh 按预期工作。我正在对其进行更多测试。
  • 我以 JSON.sh 作为解决方案。它工作得很好。谢谢
猜你喜欢
  • 1970-01-01
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 2018-07-13
  • 1970-01-01
  • 2021-04-26
  • 2016-12-25
  • 1970-01-01
相关资源
最近更新 更多