從 JSON 檔案中提取特定鍵的值

從 JSON 檔案中提取特定鍵的值

我有一個巨大的 JSON,如下所示。樣本

{
  "Key": "4fa21496-3534-4480-8405-a0f7699a915e",
  "FirstName": "XX",
  "XX": "Liggins",
  "City": "Dallas",
  "RegionCode": "TX",
  "RegionName": "Texas",
  "Country": "United States",
  "Latitude": "32.783060",
  "Longitude": "-96.806670",
  "TimeZone": "America/Chicago",
  "IsCASL": false,
  "Updated": "2018-02-28T07:32:28",
  "HQCompanyName": "IHOP",
  "Position": "Server",
  "Level": "General",
  "Department1": "General",
  "DecisionMaker": false,
  "EmailAddress": "[email protected]",
  "EmailValidationStatus": "AcceptAll",
  "EmailValidationDate": "2018-02-10T03:02:07.1692141+00:00",
  "HQCompanyId": "XX2f37e171e26112cb",
  "Experience": [
    {
      "HQCompanyId": "XX2f37e171e26112cb",
      "Title": "Server",
      "Level": "General",
      "Department1": "General",
      "DecisionMaker": false,
      "Start": "2014",
      "End": "2015",
      "EmailAddress": "[email protected]",
      "SecondaryCompanyId": "10407780-c062-40c9-8783-110aa931a9c5",
      "Updated": "2018-08-31T23:52:31.663147Z"
    },
    {
      "HQCompanyId": "128ce753-6a72-4028-8a17-d76a1c53b22e",
      "Title": "XX",
      "Level": "General",
      "Department1": "General",
      "DecisionMaker": false,
      "Start": "May 2015",
      "End": "December 2015",
      "Duration": "7 months",
      "Updated": "2018-08-31T23:52:31.663147Z"
    }
  ],
  "Education": [
    {
      "Name": "XX",
      "Degree": "XX or equivalent",
      "Start": "2004",
      "End": "2008"
    }
  ],
  "SearchUrl": "https://www.google.com/#newwindow=1&q=XX+X",
  "SourceList": []
}

我想從 json 文件中檢索所有電子郵件地址,基本上user@domainEmailAddress: "user@domain"這樣,

[email protected]
[email protected]

答案1

使用傑克,一個合適的JSON解析工具:

$ jq -r '..|.EmailAddress? //empty' infile.json
[email protected]
[email protected]

上網試試:https://jqplay.org/s/yjkKkCxecg


來自jq手冊:

--raw-output / -r
使用此選項,如果過濾器的結果是字串,那麼它將直接寫入標準輸出,而不是格式化為帶有引號的 JSON 字串。

遞迴下降:..
遞歸下降.,產生每個值。
[...]
在範例中,我們用於..|.foo?尋找在「below」中找到的任何物件中物件鍵「foo」的所有值.

可選的物件標識符索引:.foo?
就像 一樣,但當不是陣列或物件.foo時,甚至不輸出錯誤。.

替代運算子://
形式的過濾器a // b產生與 相同的結果a,如果a產生除 和 以外的false結果null。否則,a // b產生與 相同的結果b

這對於提供預設值很有用:.foo // 1將評估輸入中1是否沒有元素。.foo

empty
empty沒有回傳結果。一個都沒有。甚至不null

相關內容