简体   繁体   English

通过BASH中的JSON文件读取和循环

[英]Reading and Looping Through A JSON File in BASH

I've got a JSON file (see below) called department_groups.json. 我有一个名为department_groups.json的JSON文件(见下文)。

Essentially if I gave an argument of commercial I'd like it to return: 基本上如果我提出commercial论证,我希望它能够回归:

commercial-team@domain.com

commercial-updates@domain.com

Can anyone guide/help me with doing this? 任何人都可以指导/帮助我这样做吗?

{
    "legal": {
        "google_groups":[
            ["Legal", "legal@domain.com"],
            ["Legal Team", "legal-team@domain.com"],
            ["Compliance Checks", "compliance@domain.com"]
        ],
        "samba_groups": ""
     },
    "commercial":{
        "google_groups":[
            ["Commercial Team", "commercial-team@domain.com"],
            ["Commercial Updates", "commercial-updates@domain.com"]
        ],
        "samba_groups": ""  
    },
    "technology":{
        "google_groups":[
            ["Technology", "technology@domain.com"],
            ["Incidents", "incidents@domain.com"]
        ],
        "samba_groups": ""
    }
}

This returns the second element in each array in the google_groups property of the commercial property: 这将返回commercial属性的google_groups属性中每个数组中的第二个元素:

jq --arg key commercial '.[$key].google_groups | .[] | .[1]' file

Use jq -r to output in "raw" format (lose the double quotes). 使用jq -r以“原始”格式输出(丢失双引号)。

$ key=commercial
$ jq -r --arg key "$key" '.[$key].google_groups | .[] | .[1]' file
commercial-team@domain.com
commercial-updates@domain.com

I used --arg in these examples to show how it is used, optionally with a shell variable. 我在这些示例中使用了--arg来显示它是如何使用的,可选地使用shell变量。 If, on the other hand, commercial was just a fixed string, then you could simplify: 另一方面,如果commercial只是一个固定的字符串,那么你可以简化:

jq -r '.commercial.google_groups | .[] | .[1]' file

To process each line of the output, you can just use a shell while read loop: 要处理输出的每一行,您可以while read循环时使用shell:

key=commercial
while read -r email; do
  echo "$email"
  # process each email individually here
done < <(jq -r --arg key "$key" '.[$key].google_groups | .[] | .[1]' file)

Here I am using a process substitution <() , which acts like a file that can be processed by the shell. 这里我使用的是进程替换<() ,它就像一个可以由shell处理的文件。 One advantage of doing this, over using a pipe, is that no subshell is created. 与使用管道相比,这样做的一个优点是不会创建子shell。 Among other things, this means that the variables used within the loop remain in scope after the while block, so you can use them later. 除此之外,这意味着循环中使用的变量在while块之后仍然在范围内,因此您可以在以后使用它们。

If you prefer to use a pipe, just remove the part after done and move the command up to the first line: 如果您更喜欢使用管道,只需在done后移除部件并将命令移至第一行:

jq ... | while read -r email; do # etc.

As @TomFenech noted, the requirements are somewhat unclear, but if it's the email addresses you want, the following variant of his answer may be of interest: 正如@TomFenech指出的那样,要求有点不清楚,但如果它是你想要的电子邮件地址,他的答案的以下变体可能会引起关注:

key=commercial
$ jq -r --arg key "$key" '.[$key].google_groups[][] | select(test("@"))' department_groups.json
commercial-team@domain.com
commercial-updates@domain.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM