Json

如何讓 jq 生成 json 字典的值數組?

  • September 7, 2021

我有一json本字典

$ cat foo.json
{
 "black": "#073642",
 "red": "#DC322F",
 "green": "#859900",
 "yellow": "#CF9A6B",
 "blue": "#268BD2",
 "purple": "#D33682",
 "cyan": "#2AA198",
 "white": "#EEE8D5",
 "brightBlack": "#657B83",
 "brightRed": "#D87979",
 "brightGreen": "#88CF76",
 "brightYellow": "#657B83",
 "brightBlue": "#2699FF",
 "brightPurple": "#D33682",
 "brightCyan": "#43B8C3",
 "brightWhite": "#FDF6E3",
 "foreground": "#839496",
 "background": "#002B36",
 "cursorColor": "#839496"
}

我想用它jq來列印出這本字典中的所有。所以,輸出應該是:

$ jq 'magic' foo.json
#073642
#DC322F
#859900
#CF9A6B
#268BD2
#D33682
#2AA198
#EEE8D5
#657B83
#D87979
#88CF76
#657B83
#2699FF
#D33682
#43B8C3
#FDF6E3
#839496
#002B36
#839496

'magic'我在找什麼?

.[]對對象的作用

您也可以在對像上使用它,它將返回對象的所有值。

要獲得準確的結果,您還需要jq -r/--raw-output省略 JSON 字元串引號:

jq -r '.[]' foo.json

引用自:https://unix.stackexchange.com/questions/668092