从 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

相关内容