从JSON文件Bash shell中提取对象 [英] Bash shell extract object from json file
问题描述
我有以下的JSON文件和我正在寻找一种方式使用使用bash shell的对象路径来提取对象。
例如:
如果我说的提取物(的生产-AP / AP-东北-1')将返回我的ACCESSKEY:OO
,帐户ID:99
我喜欢的bash shell脚本,但对它的了解有限,请大家帮忙!
感谢
{
生产美联社:{
AP-东北-1:{
ACCESSKEY:OO,
帐户ID:99
},
AP-东北-2:{
ACCESSKEY:AB,
帐户ID:12
}
},
生产 - 欧盟:{
欧盟 - 西-1:{
ACCESSKEY:CD,
帐户ID:34
},
美东1:{
ACCESSKEY:CD,
帐户ID:34
}
},
生产美:{
美东1:{
ACCESSKEY:EF,
帐户ID:56
},
美东2:{
ACCESSKEY:EF,
帐户ID:56
}
},
舞台-AP:{
AP-东北-1:{
ACCESSKEY:AK
帐户ID:78
},
AP-东北-2:{
ACCESSKEY:AK
帐户ID:78
}
},
的阶段 - 欧盟:{
欧盟 - 西-1:{
ACCESSKEY:AK
帐户ID:55
},
欧盟 - 西-2:{
ACCESSKEY:AK
帐户ID:55
}
},
舞台-US:{
美东1:{
ACCESSKEY:AK
帐户ID:30
},
美东2:{
ACCESSKEY:AK
帐户ID:30
}
},
私人的: {
美西-2:{
ACCESSKEY:Z2,
帐户ID:52
},
美西-1:{
ACCESSKEY:Z2,
帐户ID:52
}
}
}
这将是片状这样使用bash。有似乎是写成shell脚本 JSON解析器,虽然我不知道他们是多么坚实。我建议像 JQ 它可以作为一个单独的程序运行,并在管道中使用。这是一个独立的可执行文件,并用C写的没有理由不能与程序一起被运。
I have a following json file and I'm looking for a way to extract object using object path using bash shell. For example:
if I say extract('production-ap/ap-northeast-1') it will return me "accessKey": "OO"
, "accountID": "99"
.
I like bash shell scripting but have limited knowledge of it, please help!
Thanks
{
"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"
}
}
}
It's going to be flaky doing this using bash. There do seem to be json parsers written as shell scripts although I'm not sure how solid they are. I'd recommend something like jq which can run as a separate program and be used in a pipe. It's a standalone executable and written in C. There's no reason it can't be shipped along with your program.
这篇关于从JSON文件Bash shell中提取对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!