僅當物件中的“ServiceTags”具有字串元素“orchestrator”時,我才需要從資料物件陣列中獲取所有不同的“節點”。
例如,對于 json:
[
{
"Node": "abc",
"ServiceTags": [],
"Type": "",
"Definition": {
"Interval": "0s",
"Timeout": "0s"
},
"CreateIndex": 11241543,
"ModifyIndex": 11241543
},
{
"Node": "xyz",
"ServiceTags": [
"rules",
"rhdm_es",
"rhdm",
"orchestrator"
],
"Type": "http",
"Definition": {
"Interval": "0s",
"Timeout": "0s"
},
"CreateIndex": 12907642,
"ModifyIndex": 12907659
},
{
"Node": "teb",
"ServiceTags": [
"rules",
"orchestrator"
],
"Type": "http",
"Definition": {
"Interval": "0s",
"Timeout": "0s"
},
"CreateIndex": 12907642,
"ModifyIndex": 12907659
},
{
"Node": "iry",
"ServiceTags": [
"rules"
],
"Type": "http",
"Definition": {
"Interval": "0s",
"Timeout": "0s"
},
"CreateIndex": 12907642,
"ModifyIndex": 12907659
}
]
預期的結果將是一個包含值“xyz”和“teb”的陣列,因為“ServiceTags”屬性中有一個“orchestrator”。
我將不勝感激,我目前正在一個僅列印所有 ServiceTag 的基本 shell 腳本中進行此操作:
# Get consul data in json format
consulResult=$(cat -)
# Validate if the json is not null or empty
if [ -z "$consulResult" ];
then
echo "NULL OR EMPTY";
else
echo "Not NULL";
echo $consulResult | jq '.[].ServiceTags'
fi
uj5u.com熱心網友回復:
map(select(.ServiceTags | index("orchestrator")).Node)
將根據陣列中是否orchestrator存在進行過濾,然后輸出這些物件ServiceTags.Node
輸出:
[
"xyz",
"teb"
]
在線嘗試!
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/515324.html
標籤:壳jq
上一篇:sqlplus-L選項
下一篇:通過Shell腳本連接XML
