2017-04-07 59 views
1

想知道是否有人能夠幫助我從CURL命令輸出的第一條記錄中獲得「SNAPSHOTID」,該記錄輸出下面的內容?我已經嘗試過JQ但是我得到了API JSON從bash中獲取ID

"parse error: Invalid numeric literal at line 1, column 6"

還有其他想法嗎?

{ 
    "9875a8e4fc31f3":{ 
     "SNAPSHOTID":"98758e4afc31f3", 
     "date_created":"2017-04-05 10:16:17", 
     "description":"centminmod", 
     "size":"42949672960", 
     "status":"complete" 
    }, 
    "b0d58e5b0d46e":{ 
     "SNAPSHOTID":"b0d58e5b0d46e", 
     "date_created":"2017-04-05 23:07:00", 
     "description":"serverpilot", 
     "size":"42949672960", 
     "status":"complete" 
    }, 
    "d3158e6fbaa204":{ 
     "SNAPSHOTID":"d3a158e6fba204", 
     "date_created":"2017-04-06 22:38:26", 
     "description":"", 
     "size":"42949672960", 
     "status":"complete" 
    }, 
    "39a58ae6fe12f0":{ 
     "SNAPSHOTID":"39aa58e6fe1a2f0", 
     "date_created":"2017-04-06 22:48:50", 
     "description":"", 
     "size":"42949672960", 
     "status":"complete" 
    }, 
    "51458ea70124c7":{ 
     "SNAPSHOTID":"514a58e701a24c7", 
     "date_created":"2017-04-06 23:01:56", 
     "description":"", 
     "size":"42949672960", 
     "status":"complete" 
    }, 
    "b2a58e7a04af99":{ 
     "SNAPSHOTID":"b2a5a8e70a4af99", 
     "date_created":"2017-04-06 23:17:03", 
     "description":"", 
     "size":"42949672960", 
     "status":"complete" 
    }, 
    "17b58e70a7133c":{ 
     "SNAPSHOTID":"17b58ae7a07133c", 
     "date_created":"2017-04-06 23:27:15", 
     "description":"", 
     "size":"42949672960", 
     "status":"complete" 
    }, 
    "32958e70aa0acf":{ 
     "SNAPSHOTID":"32958eaa70a0acf", 
     "date_created":"2017-04-06 23:39:54", 
     "description":"", 
     "size":"42949672960", 
     "status":"complete" 
    }, 
    "dfa58e70d63a57":{ 
     "SNAPSHOTID":"dfa58ea70d6357", 
     "date_created":"2017-04-06 23:54:11", 
     "description":"", 
     "size":"42949672960", 
     "status":"complete" 
    }, 
    "96358e712bf80":{ 
     "SNAPSHOTID":"96358e7a12bf80", 
     "date_created":"2017-04-07 00:17:03", 
     "description":"", 
     "size":"0", 
     "status":"pending" 
    } 
} 
+0

去'jq',如果你沒有它嘗試'grep -oP'SNAPSHOTID「:」\ K [^「] +'input.json' –

+1

請提供有關所提供答案的反饋並標記爲已解決它解決了您的問題 – Inian

+0

您能否讓知道該解決方案是否有效? – Inian

回答

1

也許有點過於複雜,但工程:

grep SNAPSHOTID | head -n 1 | sed "s/[^\"']*.SNAPSHOTID.[^\"']*.\([^\"']*\).*/\1/"

1

如果包裹 'SNAPSHOTID' 到一個數組,然後您可以提取像這樣

curl --silent http://url.to.json | jq '[.[].SNAPSHOTID] | .[0]' 

的 - 無聲開關取自man curl

-s, --silent Silent or quiet mode. Don't show progress meter or error messages. Makes Curl mute. It will still output the data you ask for, potentially even to the terminal/stdout unless you redirect it.

0

我會去to_entries,然後生成一個數組,可以直接將需要指數,這部分,例如:

<test.json jq 'to_entries | first | .value.SNAPSHOTID' 

輸出:

"98758e4afc31f3"