jq命令允许直接在命令行下对JSON进行操作,包括分片、过滤、转换等
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。基于JavaScript(Standard ECMA-262 3rd Edition - December 1999)的一个子集
JSON 数据的书写格式是: 名称/对象。
JSON的结构基于两点
值的有序列表 多数语言中被理解为数组(array)
JSON的基础结构说明
Ubuntu下的jq安装
apt-get install jq
jq命令的格式
jq [options] filter [files]
**options:**
--version:输出jq的版本信息并退出
--slurp/-s:读入整个输入流到一个数组。
--raw-input/-R:不作为JSON解析,将每一行的文本作为字符串输出到屏幕。
--null-input/ -n:不读取任何输入,过滤器运行使用null作为输入。一般用作从头构建JSON数据。
--compact-output /-c:使输出紧凑,而不是把每一个JSON对象输出在一行。
--colour-output / -C:打开颜色显示
--monochrome-output / -M:关闭颜色显示
--ascii-output /-a:指定输出格式为ASCII
-raw-output /-r :如果过滤的结果是一个字符串,那么直接写到标准输出(去掉字符串的引号)
**filter:**
. : 默认输出
.foo: 输出指定属性,foo代表属性。
.[foo] :输出指定数组元素。foo代表数组下标。
.[]:输出指定数组中全部元素
, :指定多个属性作为过滤条件时,用逗号分隔
| : 将指定的数组元素中的某个属性作为过滤条件
**files:**
JOSN格式文件。
用作示例的JOSN文件。
[{"hostCompany":"Beijing Autelan Technology","hostModel":"CS-VIC-2000-C","hostsn":"01010730b12014A00477","mac":"00:1F:64:CE:F3:8E","cpuModel":"MIPS 74Kc V4.12","cpuSN":"000000","memoryModel":"abcdefg","memorySN":"000000","boardSN":"01010730b12014A00477","networkCardMac":"00:1F:64:CE:F3:8F","lowFreModel":"AR9344","lowFreSN":"000000","hignFreModel":"AR9582","hignFreSN":"000000","gpsModel":"abcdefg","gpsSN":"000000","MEID_3g":"A000004E123ABD2","Company_3g":"ZTEMT INCORPORATED","modelOf3g":"MC271X","snOf3g":"A000004E123ABD2","iccid":"89860314400200885980","Operators":"CTCC","hardVersion":"1.20","firmwareVersion":"1.0.6.29"}]
示例:
$ jq . apinfo.json
[
{
"firmwareVersion": "1.0.6.29",
"hardVersion": "1.20",
"Operators": "CTCC",
"iccid": "DATA DATA",
"snOf3g": "A000004E123ABD2",
"modelOf3g": "MC271X",
"Company_3g": "ZTEMT INCORPORATED",
"MEID_3g": "A000004E123ABD2",
"memorySN": "000000",
"memoryModel": "abcdefg",
"cpuSN": "000000",
"cpuModel": "MIPS 74Kc V4.12",
"mac": "00:1F:64:CE:F3:8E",
"hostsn": "01010730b12014A00477",
"hostModel": "CS-VIC-1999-C",
"stCompany": "Beijing Autelan Technology",
"boardSN": "01010730b12014A00477",
"networkCardMac": "00:1F:64:CE:F3:8F",
"lowFreModel": "AR9344",
"lowFreSN": "000000",
"hignFreModel": "AR9582",
"hignFreSN": "000000",
"gpsModel": "abcdefg",
"gpsSN": "000000"
}
]
jq .[] apinfo.json
{
"firmwareVersion": "1.0.6.29",
"hardVersion": "1.20",
"Operators": "CTCC",
"iccid": "DATA DATA",
"snOf3g": "A000004E123ABD2",
"modelOf3g": "MC271X",
"Company_3g": "ZTEMT INCORPORATED",
"MEID_3g": "A000004E123ABD2",
"memorySN": "000000",
"memoryModel": "abcdefg",
"cpuSN": "000000",
"cpuModel": "MIPS 74Kc V4.12",
"mac": "00:1F:64:CE:F3:8E",
"hostsn": "01010730b12014A00477",
"hostModel": "CS-VIC-1999-C",
"stCompany": "Beijing Autelan Technology",
"boardSN": "01010730b12014A00477",
"networkCardMac": "00:1F:64:CE:F3:8F",
"lowFreModel": "AR9344",
"lowFreSN": "000000",
"hignFreModel": "AR9582",
"hignFreSN": "000000",
"gpsModel": "abcdefg",
"gpsSN": "000000"
}
$ jq -r .[].mac apinfo.json
00:1F:64:CE:F3:8E
$ jq -r '.[] | .mac' apinfo.json
00:1F:64:CE:F3:8E
$ jq -r '.[] |.mac, .gpsSN' apinfo.json
00:1F:64:CE:F3:8E
000000
jq -r '.[].mac, .[].gpsSN' apinfo.json
00:1F:64:CE:F3:8E
000000